Python中的分組函數(groupby、itertools)


 

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進行分組:

lst.sort(key=itemgetter('country')) #需要先排序,然后才能groupby。lst排序后自身被改變
lstg = groupby(lst,itemgetter('country')) 
#lstg = groupby(lst,key=lambda x:x['country']) 等同於使用itemgetter()

for key,group in lstg: for g in group: #group是一個迭代器,包含了所有的分組列表 print key,g 返回: China {'country': 'China', 'age': 20, 'name': 'zhangsan'} China {'country': 'China', 'age': 22, 'name': 'lijiu'} JP {'country': 'JP', 'age': 22, 'name': 'lisi'} USA {'country': 'USA', 'age': 19, 'name': 'wangwu'} USA {'country': 'USA', 'age': 22, 'name': 'zhaoliu'} USA {'country': 'USA', 'age': 22, 'name': 'pengqi'} print [key for key,group in lstg] #返回:['China', 'JP', 'USA'] print [(key,list(group)) for key,group in lstg] #返回的list中包含着三個元組: [('China', [{'country': 'China', 'age': 20, 'name': 'zhangsan'}, {'country': 'China', 'age': 22, 'name': 'lijiu'}]), ('JP', [{'country': 'JP', 'age': 22, 'name': 'lisi'}]), ('USA', [{'country': 'USA', 'age': 19, 'name': 'wangwu'}, {'country': 'USA', 'age': 22, 'name': 'zhaoliu'}, {'country': 'USA', 'age': 22, 'name': 'pengqi'}])] print dict([(key,list(group)) for key,group in lstg]) #返回的是一個字典: {'JP': [{'country': 'JP', 'age': 22, 'name': 'lisi'}], 'China': [{'country': 'China', 'age': 20, 'name': 'zhangsan'}, {'country': 'China', 'age': 22, 'name': 'lijiu'}], 'USA': [{'country': 'USA', 'age': 19, 'name': 'wangwu'}, {'country': 'USA', 'age': 22, 'name': 'zhaoliu'}, {'country': 'USA', 'age': 22, 'name': 'pengqi'}]} print dict([(key,len(list(group))) for key,group in lstg]) #返回每個分組的個數: {'JP': 1, 'China': 2, 'USA': 3}

 

#返回包含有2個以上元素的分組
print [key for key,group in groupby(sorted(lst,key=itemgetter('country')),itemgetter('country')) if len(list(group))>=2]
#返回:['China', 'USA']

 

lstg = groupby(sorted(lst,key=itemgetter('country')),key=itemgetter('country')) 
lstgall=[(key,list(group)) for key,group in lstg ]
print dict(filter(lambda x:len(x[1])>2,lstgall)) 
#過濾出分組后的元素個數大於2個的分組,返回:
{'USA': [{'country': 'USA', 'age': 19, 'name': 'wangwu'}, {'country': 'USA', 'age': 22, 'name': 'zhaoliu'}, {'country': 'USA', 'age': 22, 'name': 'pengqi'}]}

 

自定義分組:

from itertools import groupby
lst=[2,8,11,25,43,6,9,29,51,66]

def gb(num):
    if num <= 10:
        return 'less'
    elif num >=30:
        return 'great'
    else:
        return 'middle'

print [(k,list(g))for k,g in groupby(sorted(lst),key=gb)]
返回:
[('less', [2, 6, 8, 9]), ('middle', [11, 25, 29]), ('great', [43, 51, 66])]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM