Python中,如何打印一个迭代器Generator

**

Python中,如何打印一个迭代器Generator

**
使用sorted函数即可。
例:矩阵转置的实现

matrix=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]
m=list(zip(*matrix))
print(m)
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]

为了把元组转换成列表,我需要map函数:

f=map(list,m)
f
<map object at 0x0000000003279860>

我只要令:

g=sorted(map(list,f))
g
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

猜你喜欢

转载自blog.csdn.net/weixin_44288817/article/details/86759661