collections.Counter() Counter(计数器)追踪值的出现次数,用字典存储 (key = 值, value = 出现次数) import collections a = [1,2,3,4,1,2] print(collections.Counter(a)) >> Counter({1: 2, 2: 2, 3: 1, 4: 1}) print(collections.Counter(a).most_common(3)) >>[(1, 2), (2, 2), (3, 1)]