- #!/usr/bin/python
- from itertools import *
- class Point:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- def __repr__(self):
- return '(%s, %s)' % (self.x, self.y)
- def cmp(self, other):
- return cmp((self.x, self.y), (other.x, other.y))
- data = list(imap(Point, cycle(islice(count(), 3)), islice(count(), 7)))
- print 'Data:', data
- data.sort()
- print 'sorted Data:', data
复制代码 看书的时候看到这个问题,程序没执行的时候以为sort会对 列表data 进行排序,当执行完程序后结果如下:- Data: [(0, 0), (1, 1), (2, 2), (0, 3), (1, 4), (2, 5), (0, 6)]
- sorted Data: [(0, 3), (0, 0), (1, 1), (2, 2), (1, 4), (2, 5), (0, 6)]
复制代码 想问下这个sort的结果为什么和 自己创建的list 然后执行的sort结果不一样? |