Zipp 列表 python 通过遍历列表列表

Zipp lists in python by iterating through a list of lists

我有两个列表列表如下。要合并它们,我通常会执行以下操作:

>>>from itertools import imap, ilist

>>>a = [1,2,3]
>>>b = [4,5,6]
>>> c = list(imap(list,izip(a,b)))
>>> c 
[[1, 4]], [2, 5], [3, 6]]

但是,现在我有一个列表如下:

[[1,2,3], 
 [4,5,6],
 [7,8,9],
]

如何遍历每个列表并将其传递给 izip 函数以获得以下输出:

[[1,4,7],[2,5,8],[3,6,9]]

已编辑问题的答案:

>>> input_list=[[1,2,3], 
 [4,5,6],
 [7,8,9],
]

使用地图和 zip:

>>> map(list,zip(*input_list))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

使用 imap 和 izip:

>>> list(imap(list,list(izip(*input_list))))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

上一个问题的答案:

通过使用列表理解和两个 for 循环:

input_list =[[[1],[2],[3]], 
 [[4],[5],[6]],
 [[7],[8],[9]],
]


out_list = [[] for i in range(len(input_list))]
for each_row in input_list:
    for i in range(len(each_row)):
        out_list[i].extend(each_row[i])
print out_list

输出:

[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

我想你需要这样的东西:

input_list =[[1,2,3], 
         [4,5,6],
         [7,8,9],
        ]
result = []

for i in range(len(input_list)):
   temp = []
   for list in input_list:
      temp.append(list[i])
   result.append(temp)
print result

结果将是:

[[1, 4, 7], [2, 5, 8], [3, 6, 9]]