列表内部的字典的value进行排序
li = [{a:1,b:2,c:3,d:4},{e:5,f:6,g:7,h:8}]
li = [{"day":2},{"day":1},{"day":3}] newlist = sorted(li, key=lambda k: k['day'])
或者:
from operator import itemgetter li = [{"day":2},{"day":1},{"day":3}] newlist = sorted(li, key=itemgetter('day'))
python 中的range和xrange
xrange 函数说明:用法与range完全相同,所不同的是生成的不是一个数组,而是一个生成器。
range会直接生成一个list对象:
a = range(0,100) print type(a) print a print a[0], a[1]
结果:
<type 'list'> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] 0 1
而xrange则不会直接生成一个list,而是每次调用返回其中的一个值:
a = xrange(0,100) print type(a) print a print a[0], a[1]
结果:
<type 'xrange'> xrange(100) 0 1