我有两个列表,想将它们合并到一个tuples
. 我想用它来做list comprehension
,我可以用它来工作map
。但很高兴知道这里的列表理解将如何工作。代码在这里
>>> lst = [1,2,3,4,5]
>>> lst2 = [6,7,8,9,10]
>>> tup = map(None,lst,lst2) # works fine
>>> tup
[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
>>> l3 = [lst, lst2]
>>> l3
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
>>> zip(*l3) # works fine
[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
>>> [(i,j) for i in lst and for j in lst2] # does not work
File "<stdin>", line 1
[(i,j) for i in lst and for j in lst2]
^
SyntaxError: invalid syntax
>>>
我已经写了评论在哪里有效,在哪里无效。两个怎么能for-loop
耦合进去list comprehension