简体   繁体   English

将列表与元组列表组合在一起

[英]Combine List with List of Tuples

l1 = [1, 2, 3, 4]
l2 = [(10, 20), (30, 40), (50, 60), (70, 80)]

>>> print(list(zip(l1, l2)))

[(1, (10, 20)), (2, (30, 40)), (3, (50, 60)), (4, (70, 80))]

However, I want it to be just a list of four tuples likes this: 但是,我希望它只是一个包含四个元组的列表:

[(1, 10, 20), (2, 30, 40), (3, 50, 60), (4, 70, 80)]

I've also tried: 我也尝试过:

>>> print(list(zip(l1, *l2)))

[(1, 10, 30, 50, 70), (2, 20, 40, 60, 80)]

So my question is: 所以我的问题是:

How can I zip a list with a list of tuples or a list of lists? 如何使用元组列表或列表列表压缩列表?

A more generic way to approach this problem is to consider that both the lists should be scaled up/down to the same dimension as and when required 解决此问题的一种更通用的方法是考虑将这两个列表按比例放大/缩小到相同的维度。

>>> [(a, ) + b for a, b in zip(l1, l2)]
[(1, 10, 20), (2, 30, 40), (3, 50, 60), (4, 70, 80)]

As in your example, the second list has an extra dimension in contrast to the first list, either 如您的示例所示,第二个列表与第一个列表相比具有额外的维度

  1. Convert the first list to the higher dimension matching the second list 将第一个列表转换为与第二个列表匹配的较高维度
  2. Reduce the dimension of the second list 减少第二个列表的维度

In this particular case, the first approach was easier and thus was the obvious choice 在这种特殊情况下,第一种方法更容易,因此是明显的选择

>>> l1=[1,2,3,4]
>>> l2=[(10,20),(30,40),(50,60),(70,80)]
>>> [tuple([x] + list(y)) for x,y in zip(l1,l2)]
[(1, 10, 20), (2, 30, 40), (3, 50, 60), (4, 70, 80)]

You need 你需要

l1 = [1, 2, 3, 4]
l2 = [(10, 20), (30, 40), (50, 60), (70, 80)]
print [(a, b[0], b[1]) for a, b in list(zip(l1, l2))]

This works like this: zip creates an array that combines the elements of l1 and l2 这样工作如下: zip创建一个组合l1l2元素的数组

>>> zip (l1, l2)
[(1, (10, 20)), (2, (30, 40)), (3, (50, 60)), (4, (70, 80))]

for i, j in zip(l1, l2) will unpack the values into i and j sequentially picking up one element from each array in each loop iteration; for i, j in zip(l1, l2)将值解包为i和j,在每次循环迭代中从每个数组中依次拾取一个元素; ie, 1 and (10, 20) in the first iteration, 2 and (30, 40) in the second, etc. 即,第一次迭代中的1(10, 20) ,第二次中的2(30, 40)等。

You can then reshuffle the values to create the tuple you want. 然后,您可以重新调整值以创建所需的元组。 You can do this either with (a, b[0], b[1]) or (a, ) + b . 你可以用(a, b[0], b[1])(a, ) + b来做到这一点。 The latter approach, which Abhijit used and is probably more efficient than mine, is based on the fact that you can add tuples (eg (1, 2) + (3, 4) + (5,) equals (1, 2, 3, 4, 5) ). 后一种方法,Abhijit使用并且可能比我的更有效,是基于你可以添加元组的事实(例如(1, 2) + (3, 4) + (5,)等于(1, 2, 3, 4, 5) )。

If what you want to do with the numbers isn't overly complex, you can do it at the beginning of the for expression instead of just creating the tuple. 如果你想对数字做什么并不是太复杂,你可以在for表达式的开头而不是仅创建元组。 Maybe you can make your code more compact that way. 也许你可以通过这种方式使代码更加紧凑。

If let me do this, it will do as below: 如果让我这样做,它将如下所示:

l1 = [1, 2, 3, 4]
l2 = [(10, 20), (30, 40), (50, 60), (70, 80)]

print [ (a,b[0],b[1]) for a, b in zip(l1, l2)]

So you have to know zip() how to use: 所以你必须知道zip()如何使用:

This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. 此函数返回元组列表,其中第i个元组包含来自每个参数序列或迭代的第i个元素。 The returned list is truncated in length to the length of the shortest argument sequence. 返回的列表的长度被截断为最短参数序列的长度。 When there are multiple arguments which are all of the same length, zip() is similar to map() with an initial argument of None. 当有多个参数长度相同时,zip()类似于map(),初始参数为None。 With a single sequence argument, it returns a list of 1-tuples. 使用单个序列参数,它返回一个1元组的列表。 With no arguments, it returns an empty list. 没有参数,它返回一个空列表。

Just putting down another way with explicit for loop, assuming len(l1) == len(l2) 假设len(l1)== len(l2),只需用显式for循环放下另一种方式

for x in range(0,len(l1)):
    print (l1[x],l2[x][0],l2[x][1])

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

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