另一种变化
>>> from itertools import chain
>>> b=[['1','2','3','4','5'],['11','12','13','14','15'],['6','7','8','9','10']]
>>> sorted(chain(*b),key=int)
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
如果你想要一个整数列表,那么
>>> sorted(map(int,chain(*b)))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
速度比较
>>> t1=Timer(stmt='sorted(map(int,chain(*b)))',setup='from __main__ import b;from itertools import chain')
>>> t2=Timer(stmt='sorted(int(i) for l in b for i in l)',setup='from __main__ import b')
>>> t3=Timer(stmt='sorted(int(a) for a in chain(*b))',setup='from __main__ import b;from itertools import chain')
>>> print "%.2f usec/pass" % (1000000 * t1.timeit(number=100000)/100000)
33.23 usec/pass
>>> print "%.2f usec/pass" % (1000000 * t2.timeit(number=100000)/100000)
35.60 usec/pass
>>> print "%.2f usec/pass" % (1000000 * t3.timeit(number=100000)/100000)
36.19 usec/pass