operator.itemgetter
2018-06-26 本文已影响0人
一片冰心一生平安
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from operator import itemgetter # itemgetter用来去dict中的key,省去了使用lambda函数
from itertools import groupby # itertool还包含有其他很多函数,比如将多个list联合起来。
d1 = {'name': 'zhangsan', 'age': 20, 'country': 'China'}
d2 = {'name': 'wangwu', 'age': 19, 'country': 'USA'}
d3 = {'name': 'lisi', 'age': 22, 'country': 'JP'}
d4 = {'name': 'zhaoliu', 'age': 22, 'country': 'USA'}
d5 = {'name': 'pengqi', 'age': 22, 'country': 'USA'}
d6 = {'name': 'lijiu', 'age': 22, 'country': 'China'}
lst = [d1, d2, d3, d4, d5, d6]
# 通过country进行分组:
# 需要先排序,然后才能groupby。lst排序后自身被改变
lst.sort(key=itemgetter('country'), reverse=True)
for i in lst:
print(i)
lstg = groupby(lst, itemgetter('country'))
# lstg = groupby(lst,key=lambda x:x['country']) 等同于使用itemgetter()
print("-"*30)
for key, group in lstg:
for g in group: # group是一个迭代器,包含了所有的分组列表
print(key, g)
import operator
def cmp_datetime(a, b):
a_datetime = datetime.datetime.strptime(a, '%Y-%m-%d %H:%M:%S')
b_datetime = datetime.datetime.strptime(b, '%Y-%m-%d %H:%M:%S')
if a_datetime > b_datetime:
return -1
elif a_datetime < b_datetime:
return 1
else:
return 0
j['data'].sort(cmp=cmp_datetime, key=operator.itemgetter('publish_time'), reverse=-1)
for i in j['data']:
print str(num) + ",",
print i['id'] + ",",
print i['level'] + ",",
print i['publish_time'] + ","