最佳答案
我知道简单的列表内涵是如何运作的,例如:
[x*2 for x in range(5)] # returns [0,2,4,6,8]
我也理解了嵌套列表理解是如何工作的:
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]]
所以,当我试着这么做的时候
l1 = [100,200,300]
l2 = [0,1,2]
[x + y for x in l2 for y in l1 ]
我早就料到了:
[100,201,302]
但我有这个:
[100,200,300,101,201,301,102,202,302]
所以我有更好的办法解决这个问题,这给了我我想要的
[x + y for x,y in zip(l1,l2)]
但我不明白第一段代码中9个元素的返回