繁体   English   中英

合并2个列表列表到元组列表

[英]combine 2 lists of list to list of tuples

我试图将不同的嵌套列表合并为元组(x,y)的列表,其中x来自第一个嵌套列表,y来自第二个嵌套列表。

nested_list1 = [[1, 2, 3],[3],[0, 3],[1]]
nested_list2 = [[.0833, .0833, .0833], [.2], [.175, .175], [.2]]

当您合并它们时,它应该是:

result = [(1,.0833), (2,.0833), (3,.0833), (3,.2), (0,.175), (3,.175), (1,.2)]

我的方法是,我需要遍历列表的列表并一次将它们加入1。 我知道要遍历1个嵌套列表,如下所示:

for list in nested_list1:
    for number in list:
        print(number)

但我无法同时遍历2个嵌套列表。

for list, list in zip(nested_list1, nested_list2):
    for number, prob in zip(list,list):
        print(tuple(number, prob)) #will not work

有任何想法吗?

您可以对列表进行双重zip

lst1 = [[1, 2, 3],[3],[0, 3],[1]]
lst2 = [[.0833, .0833, .0833], [.2], [.175, .175], [.2]]

print([(u, v) for x, y in zip(lst1, lst2) for u, v in zip(x, y)])

或者使用itertools.chain.from_iterable展平列表和zip

from itertools import chain

lst1 = [[1, 2, 3],[3],[0, 3],[1]]
lst2 = [[.0833, .0833, .0833], [.2], [.175, .175], [.2]]

print(list(zip(chain.from_iterable(lst1), chain.from_iterable(lst2))))

使用itertools.chain

>>> nested_list1 = [[1, 2, 3],[3],[0, 3],[1]]
>>> nested_list2 = [[.0833, .0833, .0833], [.2], [.175, .175], [.2]]
>>> import itertools
>>> res = list(zip(itertools.chain.from_iterable(nested_list1), itertools.chain.from_iterable(nested_list2)))
>>> res
[(1, 0.0833), (2, 0.0833), (3, 0.0833), (3, 0.2), (0, 0.175), (3, 0.175), (1, 0.2)]

整理列表,然后传递给zip()

list1 = [item for sublist in nested_list1 for item in sublist]
list2 = [item for sublist in nested_list2 for item in sublist]
final = list(zip(list1, list2))

产量:

[(1, 0.0833), (2, 0.0833), (3, 0.0833), (3, 0.2), (0, 0.175), (3, 0.175), (1, 0.2)]
result = []
[result.extend(list(zip(x, y))) for x in nested_list1 for y in nested_list2]
print(result)

您的代码中有2个错误:

  1. 您对内置list两次阴影处理,并且无法区分两个变量。 不要这样
  2. 您使用tuple(x, y)创建一个由2个变量组成的tuple 这是不正确的,因为tuple仅接受一个参数。 要构造两个变量的元组,只需使用语法(x, y)

所以这将工作:

for L1, L2 in zip(nested_list1, nested_list2):
    for number, prob in zip(L1, L2):
        print((number, prob))

更惯用的是将嵌套列表展平; 例如,通过itertools.chain

from itertools import chain

res = list(zip(chain.from_iterable(nested_list1),
               chain.from_iterable(nested_list2)))

[(1, 0.0833), (2, 0.0833), (3, 0.0833), (3, 0.2), (0, 0.175), (3, 0.175), (1, 0.2)]

这支班轮将实现您想要的。

reduce(lambda x, y: x+y, [[(i, j) for i, j in zip(x,y)] for x, y in zip(nested_list1, nested_list2)])

一种方法是将两个嵌套列表都转换为完整列表,然后使用zip。 下面的示例代码:

>>> nested_list1 = [[1, 2, 3],[3],[0, 3],[1]]
>>> nested_list2 = [[.0833, .0833, .0833], [.2], [.175, .175], [.2]]
>>> new_list1 = [x for val in nested_list1 for x in val]
>>> new_list2 = [x for val in nested_list2 for x in val]
>>> print new_list1
[1, 2, 3, 3, 0, 3, 1]
>>> print new_list2
[0.0833, 0.0833, 0.0833, 0.2, 0.175, 0.175, 0.2]
>>> new_val = zip(new_list1, new_list2)
>>> print new_val
[(1, 0.0833), (2, 0.0833), (3, 0.0833), (3, 0.2), (0, 0.175), (3, 0.175), (1, 0.2)]

使用两次zip压缩列表

from functools import reduce
reduce(lambda x,y: x+y,[(zip(i,j)) for i,j in zip(nested_list1,nested_list2)])

您也可以使用chain展平

from itertools import chain
list(chain(*[(zip(i,j)) for i,j in zip(nested_list1,nested_list2)]))

产量

[(1, 0.0833), (2, 0.0833), (3, 0.0833), (3, 0.2), (0, 0.175), (3, 0.175), (1, 0.2)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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