简体   繁体   中英

How to merge two nested lists and output a list of lists of tuples?

I have two nested lists, list_a and list_b :

list_a=[['a','b'],['c','d','e'],['f','g','f']]
list_b=[[5,7],[2,3,2],[4,7,8]]

I am trying to produce list_c which would be the result of merging each element in list_a with its corresponding element in list_b eg list_a[1][1] with list_b[1][1] and list_a[1][2] with list_b[1][2] , etc. to produce a list of lists of tuples.

list_c=[[('a',5),('b',7)],[('c',2),('d',3),('e',2)],[('f',4),('g',7),('f',8)]]

I had already tried:

list_c=[]
for list1 in list_a:
    for list2 in list_b:
        list_c.append(list(zip(list1,list2)))

But the output for this is a list of lists of tuples of all possible combinations of the elements in the original lists:

[[('a', 5), ('b', 7)], [('a', 2), ('b', 3)], 
 [('a', 4), ('b', 7)], [('c', 5), ('d', 7)],...
 [('f', 2), ('g', 3), ('f', 2)], 
 [('f', 4), ('g', 7), ('f', 8)]]

I feel like I'm close to the right answer, but am missing something simple. How do I produce a list_c of the structure I am looking for? (Preserving the order matters, since I'd like to use this for working on a CSV file)

This is an extension to the question asked here: Concatenate items in two nested lists to pairs in tuples

But I've been unable to find any similar questions that don't involve flattening the resulting list into something like:

list_c=[('a',5),('b',7),('c',2),('d',3),('e',2),('f',4),('g',7),('f',8)]

You need to zip over list_a and list_b , not do a nested for-loop (a product): something to the effect of:

[list(zip(a,b)) for a,b in zip(list_a, list_b)]

In other words:

result = []
for a, b in zip(list_a, list_b):
    result.append(list(zip(a,b)))

using list comprehension

a=[['a','b'],['c','d','e'],['f','g','f']]
b=[[5,7],[2,3,2],[4,7,8]]

c = [list(zip(i,j)) for i,j in zip(a,b)]

print(c)

"""
output 


[[('a', 5), ('b', 7)],
 [('c', 2), ('d', 3), ('e', 2)],
 [('f', 4), ('g', 7), ('f', 8)]]

"""

using lambda function this can be done as

d=list(map(lambda x,y:list(zip(x,y)),a,b))
print(d) 
"""
output 

[[('a', 5), ('b', 7)],
 [('c', 2), ('d', 3), ('e', 2)],
 [('f', 4), ('g', 7), ('f', 8)]]

"""
list_c = []
for i, element_a in enumerate(list_a):
    element_b = list_b[i]
    merged_list = []
    for j in range(len(element_a)):
        merged_list.append((element_a[j], element_b[j]))
    list_c.append(merged_list)

Create new tuple for each element in each list index and append to master list

Since the best and shorter method is already taken.., another option with the good old verbose for loop:

list_c = []
for i, a in enumerate(list_a):
  tmp = []
  for j, b in enumerate(list_b[i]):
    tmp.append((a[j], b))
  list_c.append(tmp)

list_c
#=> [[('a', 5), ('b', 7)], [('c', 2), ('d', 3), ('e', 2)], [('f', 4), ('g', 7), ('f', 8)]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM