Python 带有两个列表的嵌套列表理解

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16568056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:01:14  来源:igfitidea点击:

Nested list comprehension with two lists

pythonnestedlist-comprehension

提问by André Caldas

I understand how the simple list comprehension works eg.:

我了解简单列表理解的工作原理,例如:

[x*2 for x in range(5)] # returns [0,2,4,6,8]

and also I understand how the nested list comprehesion works:

而且我理解嵌套列表理解是如何工作的:

w_list = ["i_have_a_doubt", "with_the","nested_lists_comprehensions"]

# returns the list of strings without underscore and capitalized
print [replaced.title() for replaced in [el.replace("_"," ")for el in w_list]]

so, when I tried do this

所以,当我尝试这样做时

l1 = [100,200,300]
l2 = [0,1,2]
[x + y for x in l2 for y in l1 ]

I expected this:

我期待这个:

[101,202,303]

but I got this:

但我得到了这个:

[100,200,300,101,201,301,102,202,302]

so I got a better way solve the problem, which gave me what I want

所以我找到了解决问题的更好方法,这给了我我想要的

[x + y for x,y in zip(l1,l2)]

but I didn't understood the return of 9 elements on the first code

但我没有理解第一个代码上9个元素的返回

采纳答案by rspencer

The reason it has 9 numbers is because python treats

它有 9 个数字的原因是因为 python 对待

[x + y for x in l2 for y in l1 ]

similarly to

类似于

for x in l2:
    for y in l1:
       x + y

ie, it is a nested loop

即,它是一个嵌套循环

回答by Ashwini Chaudhary

[x + y for x in l2 for y in l1 ]

is equivalent to :

相当于:

lis = []
for x in l:
   for y in l1:
      lis.append(x+y)

So for every element of lyou're iterating l2again and again, as lhas 3 elements and l1has elements so total loops equal 9(len(l)*len(l1)).

因此,对于ll2一次又一次地迭代的每个元素,因为l有 3 个元素并且l1有元素所以总循环等于 9( len(l)*len(l1))。

回答by Wesley Baugh

List comprehensions are equivalent to for-loops. Therefore, [x + y for x in l2 for y in l1 ]would become:

列表推导式等效于 for 循环。因此,[x + y for x in l2 for y in l1 ]会变成:

new_list = []
for x in l2:
    for y in l1:
        new_list.append(x + y)

Whereas zipreturns tuples containing one element from each list. Therefore [x + y for x,y in zip(l1,l2)]is equivalent to:

zip返回包含每个列表中一个元素的元组。因此[x + y for x,y in zip(l1,l2)]相当于:

new_list = []
assert len(l1) == len(l2)
for index in xrange(len(l1)):
    new_list.append(l1[index] + l2[index])

回答by oleg

this sequence

这个序列

res = [x + y for x in l2 for y in l1 ]

res = [x + y for x in l2 for y in l1 ]

is equivalent to

相当于

res =[]
for x in l2:
    for y in l1:
        res.append(x+y)

回答by Sticky

The above answers will suffice for your question but I wanted to provide you with a list comprehension solution for reference (seeing as that was your initial code and what you're trying to understand).

以上答案足以解决您的问题,但我想为您提供一个列表理解解决方案以供参考(因为这是您的初始代码以及您试图理解的内容)。

Assuming the length of both lists are the same, you could do:

假设两个列表的长度相同,你可以这样做:

[l1[i] + l2[i] for i in range(0, len(l1))]