python --统计列表中各元素出现的次数,排序,分组

统计列表中的字典

方法一:

a = [{'appreciation_id': 1341, 'level_name': 'S'}, {'appreciation_id': 1341, 'level_name': 'S'},
     {'appreciation_id': 1341, 'level_name': 'S'}, {'appreciation_id': 1342, 'level_name': 'A'},
     {'appreciation_id': 1342, 'level_name': 'A'}, {'appreciation_id': 1342, 'level_name': 'A'},
     {'appreciation_id': 1342, 'level_name': 'A'}, {'appreciation_id': 1343, 'level_name': 'B'},
     {'appreciation_id': 1344, 'level_name': 'C'}, {'appreciation_id': 1345, 'level_name': 'E'},
     {'appreciation_id': 1351, 'level_name': 'F'}]


from collections import Counter

res = Counter([i.get('level_name') for i in a if a])
print(res)

输出:
Counter({'A': 4, 'S': 3, 'B': 1, 'C': 1, 'E': 1, 'F': 1})

方法二:

a = [{'appreciation_id': 1341, 'level_name': 'S'}, {'appreciation_id': 1341, 'level_name': 'S'},
 {'appreciation_id': 1341, 'level_name': 'S'}, {'appreciation_id': 1342, 'level_name': 'A'},
 {'appreciation_id': 1342, 'level_name': 'A'}, {'appreciation_id': 1342, 'level_name': 'A'},
 {'appreciation_id': 1342, 'level_name': 'A'}, {'appreciation_id': 1343, 'level_name': 'B'},
 {'appreciation_id': 1344, 'level_name': 'C'}, {'appreciation_id': 1345, 'level_name': 'E'},
 {'appreciation_id': 1351, 'level_name': 'F'}]

result = {}
for i in a:
    if i.get('level_name') not in result:
        result[i.get('level_name')] = 1
    else:
        result[i.get('level_name')] += 1
print(result)

方法三:
a = [{'appreciation_id': 1341, 'level_name': 'S'}, {'appreciation_id': 1341, 'level_name': 'S'},
 {'appreciation_id': 1341, 'level_name': 'S'}, {'appreciation_id': 1342, 'level_name': 'A'},
 {'appreciation_id': 1342, 'level_name': 'A'}, {'appreciation_id': 1342, 'level_name': 'A'},
 {'appreciation_id': 1342, 'level_name': 'A'}, {'appreciation_id': 1343, 'level_name': 'B'},
 {'appreciation_id': 1344, 'level_name': 'C'}, {'appreciation_id': 1345, 'level_name': 'E'},
 {'appreciation_id': 1351, 'level_name': 'F'}]

result = {}
for i in a:
    result.setdefault(i.get('level_name'), 0)
    result[i.get('level_name')] += 1

print(result)

统计纯列表

方法一:

a = ['h', 'h', 'e', 'a', 'a']
result = {}
for i in a:
    result.setdefault(i, 0)
    result[i] += 1

print(result)

输出:
	{'h': 2, 'e': 1, 'a': 2}

方法二:
a = ['h','h','e','a','a']
result = {}
for i in a:
    if i not in result:
        result[i] = 1
    else:
        result[i] += 1
print(result)

输出:
	{'h': 2, 'e': 1, 'a': 2}

方法三:
	
num = [1, 1, 2, 2, 3, 4, 4, 1]
set_num = set(num)  # set()会创建一个无序且不重复的集合
for i in set_num:
    print('%d--数量:%d' % (i,num.count(i)))

方法四:

from collections import Counter
num = [1, 1, 2, 2, 3, 4, 4, 1]
new_num = Counter(num)
print(new_num)

方法五:

num = [1, 1, 2, 2, 3, 4, 4, 1]
dic_num = {}
for i in num:
    dic_num[i] = num.count(i)
for key,value in dic_num.items():
    print('%s--数量%d' % (key,value))

排序

数据

students = [
	    {'names': 'CC', "score": 100, 'height': 189},
	    {'names': 'BB', "score": 10,  'height': 169},
	    {'names': 'AA', "score": 80,  'height': 179}
]

方法一:

sorted(students, key=lambda x: x['height'])       # 正序
输出: 
[{'names': 'BB', 'score': 10, 'height': 169}, {'names': 'AA', 'score': 80, 'height': 179}, {'names': 'CC', 'score': 100, 'height': 189}]







sorted(students, key=lambda x: x['height'],reverse=True)   # 降序
输出:
[{'names': 'CC', 'score': 100, 'height': 189}, {'names': 'AA', 'score': 80, 'height': 179}, {'names': 'BB', 'score': 10, 'height': 169}]

方法二:

import heapq
print(heapq.nsmallest(3, students, key=lambda x: x['height']))  # 第一个参数是取几个值
输出:
[{'names': 'BB', 'score': 10, 'height': 169}, {'names': 'AA', 'score': 80, 'height': 179}]

方法三:(排字典的key)

sorted({1:  ' D ', 2:  ' B ', 3:  ' B ', 4:  ' E ', 5:  ' A '})
[1, 2, 3, 4, 5]

方法四:

sorted( " This is a test string from Andrew ".split(), key=str.lower)
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']

分组

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])]
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

像风一样的男人@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值