Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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中组合2d列表元素-考虑顺序_Python_List_Multidimensional Array - Fatal编程技术网

如何在python中组合2d列表元素-考虑顺序

如何在python中组合2d列表元素-考虑顺序,python,list,multidimensional-array,Python,List,Multidimensional Array,我有一个Python脚本,它将解析文件中的一些值,并将其存储在2d列表中,如下所示: [['x'], ['Y'], ['Z']] [['99'], ['88'], ['77']] [['host1'], ['host2'], ['host3']] [['a', 'b', 'c','d','e'], ['f, 'g', 'h','i','j'], ['k', 'l', 'm','n','o']] 我想要实现的就是按照以下顺序将这些列表合并到一个大列表中 [[['X'],['99'],['h

我有一个Python脚本,它将解析文件中的一些值,并将其存储在2d列表中,如下所示:

[['x'], ['Y'], ['Z']]

[['99'], ['88'], ['77']]

[['host1'], ['host2'], ['host3']]

[['a', 'b', 'c','d','e'], ['f, 'g', 'h','i','j'], ['k', 'l', 'm','n','o']]
我想要实现的就是按照以下顺序将这些列表合并到一个大列表中

[[['X'],['99'],['host1'], ['a','b','c','d','e']], [['Y'], ['88'], ['host2'], ['f','g','h','i',j]]] etc .. 
我在stackoverflow搜索过这样的问题,但没有找到

任何帮助都将不胜感激


谢谢

让我们来定义您的列表:

>>> a = [['x'], ['Y'], ['Z']]
>>> b = [['99'], ['88'], ['77']]
>>> c = [['host1'], ['host2'], ['host3']]
>>> d = [['a', 'b', 'c','d','e'], ['f', 'g', 'h','i','j'], ['k', 'l', 'm','n','o']]
我相信这正是你想要的:

>>> zip(a, b, c, d)
[(['x'], ['99'], ['host1'], ['a', 'b', 'c', 'd', 'e']), (['Y'], ['88'], ['host2'], ['f', 'g', 'h', 'i', 'j']), (['Z'], ['77'], ['host3'], ['k', 'l', 'm', 'n', 'o'])]

让我们定义您的列表:

>>> a = [['x'], ['Y'], ['Z']]
>>> b = [['99'], ['88'], ['77']]
>>> c = [['host1'], ['host2'], ['host3']]
>>> d = [['a', 'b', 'c','d','e'], ['f', 'g', 'h','i','j'], ['k', 'l', 'm','n','o']]
我相信这正是你想要的:

>>> zip(a, b, c, d)
[(['x'], ['99'], ['host1'], ['a', 'b', 'c', 'd', 'e']), (['Y'], ['88'], ['host2'], ['f', 'g', 'h', 'i', 'j']), (['Z'], ['77'], ['host3'], ['k', 'l', 'm', 'n', 'o'])]

让我们定义您的列表:

>>> a = [['x'], ['Y'], ['Z']]
>>> b = [['99'], ['88'], ['77']]
>>> c = [['host1'], ['host2'], ['host3']]
>>> d = [['a', 'b', 'c','d','e'], ['f', 'g', 'h','i','j'], ['k', 'l', 'm','n','o']]
我相信这正是你想要的:

>>> zip(a, b, c, d)
[(['x'], ['99'], ['host1'], ['a', 'b', 'c', 'd', 'e']), (['Y'], ['88'], ['host2'], ['f', 'g', 'h', 'i', 'j']), (['Z'], ['77'], ['host3'], ['k', 'l', 'm', 'n', 'o'])]

让我们定义您的列表:

>>> a = [['x'], ['Y'], ['Z']]
>>> b = [['99'], ['88'], ['77']]
>>> c = [['host1'], ['host2'], ['host3']]
>>> d = [['a', 'b', 'c','d','e'], ['f', 'g', 'h','i','j'], ['k', 'l', 'm','n','o']]
我相信这正是你想要的:

>>> zip(a, b, c, d)
[(['x'], ['99'], ['host1'], ['a', 'b', 'c', 'd', 'e']), (['Y'], ['88'], ['host2'], ['f', 'g', 'h', 'i', 'j']), (['Z'], ['77'], ['host3'], ['k', 'l', 'm', 'n', 'o'])]

正如你得到的优秀答案中已经提到的,使用
zip

a=[['x'], ['Y'], ['Z']]
b=[['99'], ['88'], ['77']]
c=[['host1'], ['host2'], ['host3']]
d=[['a', 'b', 'c','d','e'], ['f', 'g', 'h','i','j'], ['k', 'l', 'm','n','o']]

z=[list(el) for el in zip(a,b,c,d)]
print z
产生

[[['x'], ['99'], ['host1'], ['a', 'b', 'c', 'd', 'e']], [['Y'], ['88'], ['host2'], ['f', 'g', 'h', 'i', 'j']], [['Z'], ['77'], ['host3'], ['k', 'l', 'm', 'n', 'o']]]
这是一个列表列表,而不是元组列表(如问题所示)

如果您只需要对元素进行迭代并从中读取,那么实际上没有什么区别。
另一方面,如果您需要更改元素并“就地”更改它们,列表允许您这样做(它们是可变的),而元组则不允许这样做。

正如您得到的优秀答案中已经提到的,请使用
zip

a=[['x'], ['Y'], ['Z']]
b=[['99'], ['88'], ['77']]
c=[['host1'], ['host2'], ['host3']]
d=[['a', 'b', 'c','d','e'], ['f', 'g', 'h','i','j'], ['k', 'l', 'm','n','o']]

z=[list(el) for el in zip(a,b,c,d)]
print z
产生

[[['x'], ['99'], ['host1'], ['a', 'b', 'c', 'd', 'e']], [['Y'], ['88'], ['host2'], ['f', 'g', 'h', 'i', 'j']], [['Z'], ['77'], ['host3'], ['k', 'l', 'm', 'n', 'o']]]
这是一个列表列表,而不是元组列表(如问题所示)

如果您只需要对元素进行迭代并从中读取,那么实际上没有什么区别。
另一方面,如果您需要更改元素并“就地”更改它们,列表允许您这样做(它们是可变的),而元组则不允许这样做。

正如您得到的优秀答案中已经提到的,请使用
zip

a=[['x'], ['Y'], ['Z']]
b=[['99'], ['88'], ['77']]
c=[['host1'], ['host2'], ['host3']]
d=[['a', 'b', 'c','d','e'], ['f', 'g', 'h','i','j'], ['k', 'l', 'm','n','o']]

z=[list(el) for el in zip(a,b,c,d)]
print z
产生

[[['x'], ['99'], ['host1'], ['a', 'b', 'c', 'd', 'e']], [['Y'], ['88'], ['host2'], ['f', 'g', 'h', 'i', 'j']], [['Z'], ['77'], ['host3'], ['k', 'l', 'm', 'n', 'o']]]
这是一个列表列表,而不是元组列表(如问题所示)

如果您只需要对元素进行迭代并从中读取,那么实际上没有什么区别。
另一方面,如果您需要更改元素并“就地”更改它们,列表允许您这样做(它们是可变的),而元组则不允许这样做。

正如您得到的优秀答案中已经提到的,请使用
zip

a=[['x'], ['Y'], ['Z']]
b=[['99'], ['88'], ['77']]
c=[['host1'], ['host2'], ['host3']]
d=[['a', 'b', 'c','d','e'], ['f', 'g', 'h','i','j'], ['k', 'l', 'm','n','o']]

z=[list(el) for el in zip(a,b,c,d)]
print z
产生

[[['x'], ['99'], ['host1'], ['a', 'b', 'c', 'd', 'e']], [['Y'], ['88'], ['host2'], ['f', 'g', 'h', 'i', 'j']], [['Z'], ['77'], ['host3'], ['k', 'l', 'm', 'n', 'o']]]
这是一个列表列表,而不是元组列表(如问题所示)

如果您只需要对元素进行迭代并从中读取,那么实际上没有什么区别。
另一方面,如果您需要更改元素并“就地”更改它们,列表允许您这样做(它们是可变的),而元组则不允许这样做。

@Pynchia也可以使用列表而不是tupleit获得答案。这很简单,只需将元组转换为列表:
z=[list(el)for el in zip(a,b,c,d)]
@Pynchia如果能用列表而不是tupleit得到答案也很好这很简单,只要把tuple转换成一个列表:
z=[list(el)for el in zip(a,b,c,d)]
@Pynchia如果能用列表而不是tupleit得到答案也很好只要把tuple转换成一个列表:
z=[list(el)对于zip中的el(a,b,c,d)]
@Pynchia也很高兴能用列表而不是tupleit得到答案。很简单,只要把tuple转换成一个列表:
z=[list(el)for el-in-zip(a,b,c,d)]