Skip to content Skip to sidebar Skip to footer

Create A List Of Tuples From Two Nested Lists

Having a list A with an arbitrary degree of nesting, and a list B with a nesting structure equivalent to that of A (or deeper), how can we create a list of tuples for all correspon

Solution 1:

Basically, all you need to do is, iterate a and b simultaneously and return the values of a and b, if the current element of a is not a list. Since your structure is nested, we can't lineraly iterate them. That is why we use recursion.

This solution assumes that there is always an corresponding element in B for every element in A.

defrec(a, b):
    ifisinstance(a, list):
        # If `a` is a listfor index, item inenumerate(a):
            # then recursively iterate itfor items in rec(item, b[index]):
                yield items
    else:
        # If `a` is not a list, just yield the current `a` and `b`yield a, b

print(list(rec(['a', ['b', ['c', 'd']], 'e'], [1, [2, [3, [4, 5]]], 6])))
# [('a', 1), ('b', 2), ('c', 3), ('d', [4, 5]), ('e', 6)]

Solution 2:

You want a recursive zip function:

from itertools import izip
defrecurse_zip(a, b):
    zipped = izip(a, b)
    for t in zipped:
        ifisinstance(t[0], list):
            for item in recurse_zip(*t):
                yield item
        else:
            yield t

Demo:

>>> A = ['a', ['b', ['c', 'd']], 'e'] 
>>> B = [1, [2, [3, [4, 5]]], 6]
>>> print(list(recurse_zip(A, B)))
[('a', 1), ('b', 2), ('c', 3), ('d', [4, 5]), ('e', 6)]

Notes:

  • izip is helpful to make it lazy -- python3.x's zip would work just as well.
  • Can use yield from syntax in python3.3+ (yield from recurse_zip(*t)).
  • Generators are awesome.

Solution 3:

You can iterate with zip to create a list,

A = ['a', ['b', ['c', 'd']], 'e'] 
B = [1, [2, [3, [4, 5]]], 6]

defmake_tuples(list1, list2):
    tups = []
    def_helper(l1, l2):
        for a, b inzip(l1, l2):
            ifisinstance(a, list) andisinstance(b, list):
                _helper(a, b)
            else:
                tups.append((a, b))
    _helper(list1, list2)
    return tups

make_tuples(A, B)

Or a simple tuples generator -

deftuples_generator(l1, l2):
    for a, b inzip(l1, l2):
        ifisinstance(a, list) andisinstance(b, list):
            tuples_generator(a, b)
        else:
            yield (a, b)

In : make_tuples(A, B)
Out: [('a', 1), ('b', 2), ('c', 3), ('d', [4, 5]), ('e', 6)]

Solution 4:

you can use zip, this is my answer.

a = ['a', ['b', ['c', 'd']], 'e']
b = [1, [2, [3, [4, 5]]], 6]
c = []

defCheckIfList(a):
    for k in a:
        print'k is:', k
        ifisinstance(k, (list, tuple)):
            returnTruereturnFalsedefZipAllElements(a, b, c):
    if CheckIfList(a):
        r = zip(a, b)
        for i in r:
            ifisinstance(i[0], (list, tuple)):
                ZipAllElements(i[0], i[1], c)
            else:
                c.append(i)
    else:
        c.extend(list(zip(a, b)))

ZipAllElements(a, b, c)
print c

Solution 5:

In [3]: from compiler.ast import flatten

In [4]: zip(flatten(A), flatten(B))
Out[4]: [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]

Post a Comment for "Create A List Of Tuples From Two Nested Lists"