0

我有一个这样的列表列表:

a = d[:3]
>>> a
[[90, 26.7328829998, 131075.449348, 473898.809493],
 [90, 78.2985549184, 131116.812991,473929.491012],
 [90, 132.4760969, 131157.881628, 473964.821961]]

要更改它的顺序,我将 BIFzip*-operator 结合使用。这给了我一个元组列表:

>>> b =zip(*a)
>>> b
[(90, 90, 90),
 (26.7328829998, 78.2985549184, 132.4760969),
 (131075.449348, 131116.812991, 131157.881628),
 (473898.809493, 473929.491012, 473964.821961)]

我不仅想恢复项目的原始顺序,例如:

>>> c = zip(*b)
>>> c
[(90, 26.7328829998, 131075.449348, 473898.809493),
 (90, 78.2985549184, 131116.812991, 473929.491012),
 (90, 132.4760969, 131157.881628, 473964.821961)] 

但也想取回原始类型,例如list of lists。但是这个技巧zip(*list(b))也没有list(zip(*b))(我没想到第二个片段会)。有谁知道如何在不遍历列表的情况下到达那里!?

4

2 回答 2

1

izip返回一个生成器,因此您在创建时不会循环两次c。在 Python 3 zip(also map, filter, etc.) 中默认返回一个生成器并izip已被删除。

>>> from itertools import izip
>>> a = [[90, 26.7328829998, 131075.449348, 473898.809493], [90, 78.2985549184, 131116.812991,473929.491012], [90, 132.4760969, 131157.881628, 473964.821961]]
>>> b = zip(*a)
>>> c = map(list, izip(*b))
>>> c
[[90, 26.7328829998, 131075.449348, 473898.809493], [90, 78.2985549184, 131116.812991, 473929.491012], [90, 132.4760969, 131157.881628, 473964.821961]]
于 2012-07-25T08:42:21.963 回答
0

请尝试以下任一方法:

使用地图:

c = map(list, zip(*b))

或列表理解:

c = [list(i) for i in zip(*b)]
于 2012-07-25T08:43:50.170 回答