在python 3中重复排序

1 python sorting python-3.x

我试图理解为什么zip在python 3中的对象上使用sorted函数不能多次执行.它只是第二次返回一个空列表.

In [34]: X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"] 
    ...: Y = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]
    ...: yx = zip(Y, X) 
    ...: yx 
    ...: [(0, 'a'), (1, 'b'), (1, 'c'), (0, 'd'), (1, 'e'), (2, 'f'), (2, 'g'), 
    ...: (0, 'h'), (1, 'i')] 
    ...: yx_sorted=sorted(yx)
    ...: 

In [35]: yx_sorted
Out[35]: 
[(0, 'a'),
 (0, 'd'),
 (0, 'h'),
 (1, 'b'),
 (1, 'c'),
 (1, 'e'),
 (1, 'i'),
 (2, 'f'),
 (2, 'g')]

In [36]: yx_sorted=sorted(yx)

In [37]: yx_sorted
Out[37]: []

In [38]: yx
Out[38]: <zip at 0x10476aa88>
Run Code Online (Sandbox Code Playgroud)

yx 就我所见,它依然存在.

Dan*_*man 5

zip 是Python 3中的一个生成器.一旦你迭代了一次,就会筋疲力尽.