Python 使用逐个元素将两个列表合并为一个
如何使用以下逐个元素将两个列表合并为一个:Python 使用逐个元素将两个列表合并为一个,python,Python,如何使用以下逐个元素将两个列表合并为一个: list1 = ['a','c','e'] list2 = ['apple','carrot','elephant'] result = ['a', 'apple', 'c', 'carrot', 'e', 'elephant'] 审判 result = [(x,y) for x,y in zip(list1,list2)] print result 但它们是元组,任何更简单的解析都是可能的…您可以使用双循环来理解循环列表: In [4]: l
list1 = ['a','c','e']
list2 = ['apple','carrot','elephant']
result = ['a', 'apple', 'c', 'carrot', 'e', 'elephant']
审判
result = [(x,y) for x,y in zip(list1,list2)]
print result
但它们是元组,任何更简单的解析都是可能的…您可以使用双循环来理解循环列表:
In [4]: list1 = ['a','c','e']
In [5]: list2 = ['apple','carrot','elephant']
In [6]: list(itertools.chain.from_iterable(zip(list1, list2)))
Out[6]: ['a', 'apple', 'c', 'carrot', 'e', 'elephant']
>>> list1 = ['a','c','e']
>>> list2 = ['apple','carrot','elephant']
>>> [x for z in zip(list1, list2) for x in z]
['a', 'apple', 'c', 'carrot', 'e', 'elephant']
您可以使用双循环来理解循环列表:
>>> list1 = ['a','c','e']
>>> list2 = ['apple','carrot','elephant']
>>> [x for z in zip(list1, list2) for x in z]
['a', 'apple', 'c', 'carrot', 'e', 'elephant']