如何组合不同长度重复元素的列表?

时间:2021-12-01 23:17:52

so I have say two lists:

所以我说两个清单:

list1 = [1,2,3]

and

list2 = [4]

and I need to combine them to produce the following output:

我需要将它们组合起来以产生以下输出:

list3=[[1,4],[2,4],[3,4]]

itertools doesn't seem to have a method to accomplish this, the zip function ends when the second list does...

itertools似乎没有一个方法来实现这一点,zip函数在第二个列表时结束...

I'm sure there's a one liner out there, but I'm finding too much stuff about similar but not the same problems on here and google.

我确定那里有一个班轮,但是我发现这里和谷歌有类似但不一样的问题。

Thanks for any help!

谢谢你的帮助!

5 个解决方案

#1


6  

You can iterate over the list and concatenate the list2 value and the element for the current iteration:

您可以迭代列表并连接list2值和当前迭代的元素:

list1 = [1,2,3]
list2 = [4]
new_list = [[a]+list2 for a in list1]

Output:

输出:

[[1, 4], [2, 4], [3, 4]]

Or, an alternative, although lower solution using map:

或者,另一种方法,尽管使用map的较低解决方案:

final_list = map(lambda x:[x, list2[0]], list1)

Output:

输出:

[[1, 4], [2, 4], [3, 4]]

#2


6  

You can use itertools izip_longest (py2) or itertools zip longest (py3) too:

你也可以使用itertools izip_longest(py2)或者itertools zip longest(py3):

import itertools

list(map(list,itertools.izip_longest([], list1, fillvalue=list2[0])))

Returns:

返回:

[[4, 1], [4, 2], [4, 3]]

#3


3  

Do you need a third alternative?

你需要第三种选择吗?

>>> list(map(list,zip(list2 * len(list1), list1)))
[[4, 1], [4, 2], [4, 3]]

#4


0  

You can also try using itertools.cycle():

您也可以尝试使用itertools.cycle():

>>> import itertools    
>>> list1 = [1,2,3]
>>> list2 = [4]
>>> print([list(x) for x in zip(list1, itertools.cycle(list2))])
[[1, 4], [2, 4], [3, 4]]

#5


-1  

Are you looking for something like this?

你在找这样的东西吗?

Without any external module or heavy code:

没有任何外部模块或重型代码:

print(list(map(lambda x:[x,list2[0]],list1)))

when data is :

当数据是:

list1 = [1,2,3]
list2 = [4]

output:

输出:

[[1, 4], [2, 4], [3, 4]]

As someone pointed out this is already given answer ,Here is another solution:

正如有人指出这已经给出答案,这是另一种解决方案:

list1 = [1,2,3]
list2 = [4]

print(list(zip(list1,list2*len(list1))))

output:

输出:

[(1, 4), (2, 4), (3, 4)]

#1


6  

You can iterate over the list and concatenate the list2 value and the element for the current iteration:

您可以迭代列表并连接list2值和当前迭代的元素:

list1 = [1,2,3]
list2 = [4]
new_list = [[a]+list2 for a in list1]

Output:

输出:

[[1, 4], [2, 4], [3, 4]]

Or, an alternative, although lower solution using map:

或者,另一种方法,尽管使用map的较低解决方案:

final_list = map(lambda x:[x, list2[0]], list1)

Output:

输出:

[[1, 4], [2, 4], [3, 4]]

#2


6  

You can use itertools izip_longest (py2) or itertools zip longest (py3) too:

你也可以使用itertools izip_longest(py2)或者itertools zip longest(py3):

import itertools

list(map(list,itertools.izip_longest([], list1, fillvalue=list2[0])))

Returns:

返回:

[[4, 1], [4, 2], [4, 3]]

#3


3  

Do you need a third alternative?

你需要第三种选择吗?

>>> list(map(list,zip(list2 * len(list1), list1)))
[[4, 1], [4, 2], [4, 3]]

#4


0  

You can also try using itertools.cycle():

您也可以尝试使用itertools.cycle():

>>> import itertools    
>>> list1 = [1,2,3]
>>> list2 = [4]
>>> print([list(x) for x in zip(list1, itertools.cycle(list2))])
[[1, 4], [2, 4], [3, 4]]

#5


-1  

Are you looking for something like this?

你在找这样的东西吗?

Without any external module or heavy code:

没有任何外部模块或重型代码:

print(list(map(lambda x:[x,list2[0]],list1)))

when data is :

当数据是:

list1 = [1,2,3]
list2 = [4]

output:

输出:

[[1, 4], [2, 4], [3, 4]]

As someone pointed out this is already given answer ,Here is another solution:

正如有人指出这已经给出答案,这是另一种解决方案:

list1 = [1,2,3]
list2 = [4]

print(list(zip(list1,list2*len(list1))))

output:

输出:

[(1, 4), (2, 4), (3, 4)]