Python数组统计排序问题

来源:互联网 发布:matlab读取excel数据 编辑:程序博客网 时间:2024/06/08 07:05

这里使用Counter这个类,很容易,只要采用使用lambda函数,很容易解决

from collections import Countertimes_intervals = [1,2,3,4,5,5,6,6]times_count = Counter(times_intervals)print times_counttimes_sorted = sorted(times_count.items(), key=lambda x:x[0])print times_sortedprint [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]
原创粉丝点击