成对附加在没有ZIP的python中[重复]

时间:2022-05-24 22:05:53

This question already has an answer here:

这个问题在这里已有答案:

I am currently learning list comprehension in Python. How would I do the following:

我目前正在学习Python中的列表理解。我该怎么做:

l1 = [2,4,6,8]
l2 = [2,3,4,5]
l = [*some list comprehension*]

so that

l = [[2,2],[4,3],[6,4],[8,5]]

EDIT: Can I do this without zip?

编辑:我可以没有拉链吗?

3 个解决方案

#1


2  

You want the zip function.

你想要zip功能。

Example -

>>> l1 = [2,4,6,8]
>>> l2 = [2,3,4,5]
>>>
>>> l = list(zip(l1,l2))
>>> l
[(2, 2), (4, 3), (6, 4), (8, 5)]

If you want the inner lists to be of type list instead of tuple -

如果你想内部列表是类型列表而不是元组 -

>>> l = [list(x) for x in zip(l1,l2)]
>>> l
[[2, 2], [4, 3], [6, 4], [8, 5]]

In python 3.x, zip returns an iterator, so if you do not want a list, but just want to iterate over each combined (zipped) element, you can just directly use - zip(l1,l2) .

在python 3.x中,zip返回一个迭代器,所以如果你不想要一个列表,但只是想迭代每个组合(zipped)元素,你可以直接使用 - zip(l1,l2)。

As it is asked in the question, to do it without zip function, you can use enumerate function to get the index as well as the element from one list and then use the index to get the element from second list.

正如在问题中要求的那样,没有zip函数,你可以使用枚举函数来获取索引以及一个列表中的元素,然后使用索引从第二个列表中获取元素。

>>> l = [[x,l2[i]] for i,x in enumerate(l1)]
>>> l
[[2, 2], [4, 3], [6, 4], [8, 5]]

But this would not work unless both lists have same size.Also not sure why you would want to do it without zip .

但是这不会起作用,除非两个列表都有相同的大小。也不确定为什么你想要没有拉链。

#2


0  

Using list comprehension and zip:

使用列表理解和zip:

>>> l1 = [2, 4, 6, 8]
>>> l2 = [2, 3, 4, 5]
>>> [[x, y] for x, y in zip(l1, l2)]
[[2, 2], [4, 3], [6, 4], [8, 5]]

#3


0  

You can use zip as

您可以使用zip作为

>>> l1 = [2,4,6,8]
>>> l2 = [2,3,4,5]
>>> zip(l1,l2)
[(2, 2), (4, 3), (6, 4), (8, 5)]
>>> [ list(x) for x in zip(l1,l2) ]
[[2, 2], [4, 3], [6, 4], [8, 5]]

#1


2  

You want the zip function.

你想要zip功能。

Example -

>>> l1 = [2,4,6,8]
>>> l2 = [2,3,4,5]
>>>
>>> l = list(zip(l1,l2))
>>> l
[(2, 2), (4, 3), (6, 4), (8, 5)]

If you want the inner lists to be of type list instead of tuple -

如果你想内部列表是类型列表而不是元组 -

>>> l = [list(x) for x in zip(l1,l2)]
>>> l
[[2, 2], [4, 3], [6, 4], [8, 5]]

In python 3.x, zip returns an iterator, so if you do not want a list, but just want to iterate over each combined (zipped) element, you can just directly use - zip(l1,l2) .

在python 3.x中,zip返回一个迭代器,所以如果你不想要一个列表,但只是想迭代每个组合(zipped)元素,你可以直接使用 - zip(l1,l2)。

As it is asked in the question, to do it without zip function, you can use enumerate function to get the index as well as the element from one list and then use the index to get the element from second list.

正如在问题中要求的那样,没有zip函数,你可以使用枚举函数来获取索引以及一个列表中的元素,然后使用索引从第二个列表中获取元素。

>>> l = [[x,l2[i]] for i,x in enumerate(l1)]
>>> l
[[2, 2], [4, 3], [6, 4], [8, 5]]

But this would not work unless both lists have same size.Also not sure why you would want to do it without zip .

但是这不会起作用,除非两个列表都有相同的大小。也不确定为什么你想要没有拉链。

#2


0  

Using list comprehension and zip:

使用列表理解和zip:

>>> l1 = [2, 4, 6, 8]
>>> l2 = [2, 3, 4, 5]
>>> [[x, y] for x, y in zip(l1, l2)]
[[2, 2], [4, 3], [6, 4], [8, 5]]

#3


0  

You can use zip as

您可以使用zip作为

>>> l1 = [2,4,6,8]
>>> l2 = [2,3,4,5]
>>> zip(l1,l2)
[(2, 2), (4, 3), (6, 4), (8, 5)]
>>> [ list(x) for x in zip(l1,l2) ]
[[2, 2], [4, 3], [6, 4], [8, 5]]