您的位置:首页 > 编程语言 > Python开发

Python数组统计排序问题

2017-10-05 11:46 302 查看
这里使用Counter这个类,很容易,只要采用使用lambda函数,很容易解决

from collections import Counter
times_intervals = [1,2,3,4,5,5,6,6]
times_count = Counter(times_intervals)
print times_count
times_sorted = sorted(times_count.items(), key=lambda x:x[0])
print times_sorted
print [t[0] for t in times_sorted]
>>>
Counter({5: 2, 6: 2, 1: 1, 2: 1, 3: 1, 4: 1})
[(1, 1), (2, 1), (3, 1), (4, 1), (5, 2), (6, 2)]
[1, 2, 3, 4, 5, 6]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lambda 统计 排序