两个嵌套列表合成一个列表。

huangapple go评论80阅读模式
英文:

two nested list make one list

问题

我想要一一对应的输出,就像这样的输出:
[(1, 11), (2, 22), (3, 33), (4, 44)]
我该如何修复这个问题?

英文:

Here are 2 list below

list1 = [[1,2],[3,4]]
list2 = [[11,22],[33,44]]

I tried to this

output =list(tuple(zip(i, j)) for i, j in zip(list1, list2))

But my output is not as desired.

[((1, 11), (2, 22)), ((3, 33), (4, 44))]

I want to one to one correspondence such as output like

[(1,11),(2,22),(3,33),(4,44)] 

how can I fix this?

答案1

得分: 2

你的原始代码生成了一个元组的元组列表,因为你使用了外部的 list(),一个 tuple()zip(),它生成了实际的元组 -- 你想要去掉中间的 tuple(),而是只使用一个单一的列表推导式来捕获 zip(i, j) 产生的所有元组。

你可以通过在列表推导式中使用两个 for 语句(不需要包装在 tuple() 调用中)来实现这个目标:

list1 = [[1,2],[3,4]]
list2 = [[11,22],[33,44]]
[z for i, j in zip(list1, list2) for z in zip(i, j)]

结果将会是:

[(1, 11), (2, 22), (3, 33), (4, 44)]
英文:

Your original code generates a list of tuples of tuples because you have an outer list(), a tuple(), and zip() which generates the actual tuples -- you want to get rid of that tuple() in the middle and instead just have a single list comprehension that captures all the tuples produced by zip(i, j).

You can do this by putting two for statements in the comprehension (not wrapping either in a tuple() call):

>>> list1 = [[1,2],[3,4]]
>>> list2 = [[11,22],[33,44]]
>>> [z for i, j in zip(list1, list2) for z in zip(i, j)]
[(1, 11), (2, 22), (3, 33), (4, 44)]

答案2

得分: 1

你也可以使用一个for循环来这样做:

k = []
for x, y in enumerate(list1):
    k.append(tuple(zip(y, list2[x]))[0])
    k.append(tuple(zip(y, list2[x]))[1])

# k
[(1, 11), (2, 22), (3, 33), (4, 44)]
英文:

you can do like this as well with one for loop:

k=[]
for x,y in enumerate(list1):
    k.append(tuple(zip(y,list2[x]))[0])
    k.append(tuple(zip(y,list2[x]))[1])

#k
[(1, 11), (2, 22), (3, 33), (4, 44)]

答案3

得分: 1

使用 Numpy

尝试这个使用 numpy 的解决方案 -

import numpy as np

np.array(list1+list2).reshape(2,-1).T.tolist()
[[1, 11], [2, 22], [3, 33], [4, 44]]

如果你需要内部列表变成元组,可以尝试这个变种。

import numpy as np

list(map(tuple, np.array(list1+list2).reshape(2,-1).T))
[(1, 11), (2, 22), (3, 33), (4, 44)]
英文:

Using Numpy

Try this numpy solution -

import numpy as np

np.array(list1+list2).reshape(2,-1).T.tolist()
[[1, 11], [2, 22], [3, 33], [4, 44]]

If you need the internal lists to be tuples, do this variation.

import numpy as np

list(map(tuple, np.array(list1+list2).reshape(2,-1).T))
[(1, 11), (2, 22), (3, 33), (4, 44)]

答案4

得分: -1

#python两个嵌套列表的交集程序
import itertools
import functools

def GCI(lst1, lst2):

temp1 = functools.reduce(lambda a, b: set(a).union(set(b)), lst1)
temp2 = functools.reduce(lambda a, b: set(a).union(set(b)), lst2)
 
lst3 = [list(set(a).intersection(set(b)))
       for a, b in itertools.product(lst1, lst2)
       if len(set(a).intersection(set(b))) != 0]
 
lst3.extend([x] for x in temp1.symmetric_difference(temp2))
 
return lst3

希望对您有帮助.....

英文:

#python program for intersection of two nested lists
import itertools
import functools

def GCI(lst1, lst2):

temp1 = functools.reduce(lambda a, b: set(a).union(set(b)), lst1)
temp2 = functools.reduce(lambda a, b: set(a).union(set(b)), lst2)
 
lst3 = [list(set(a).intersection(set(b)))
       for a, b in itertools.product(lst1, lst2)
       if len(set(a).intersection(set(b))) != 0]
 
lst3.extend([x] for x in temp1.symmetric_difference(temp2))
 
return lst3

Hope it helps.....

huangapple
  • 本文由 发表于 2023年1月8日 23:51:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定