Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用逐个元素将两个列表合并为一个_Python - Fatal编程技术网

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']