我了解简单列表理解的工作原理,例如:
[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 个元素的返回