按项目计数获得前几个项目

时间:2013-12-16 15:06:14

标签: python arrays list numpy

source array:
a1 = [1,2,2,2,3,4,]
a2 = [3,5,6,7,8,8,]
a3 = [3,4,7,8,9,9,]
a4 = [2,3,5,7,8,9,]

number     count
  1          1
  2          4
  3          4
  4          2
  5          2
  6          1
  7          3
  8          4
  9          3

我希望按计数获得前4个数字顺序。并返回列表类型。结果将是[2,3,8,7][2,3,8,9]

我尝试在Python中使用数组函数。但发现它没有效率。所以我找到了numpy。但我不熟悉numpy。任何人都可以通过numpy获得结果吗?或者还有其他更有效的方法吗?

1 个答案:

答案 0 :(得分:4)

在此使用collections.Counter() object

from collections import Counter

counts = Counter(a1)
for lst in (a2, a3, a4):
    counts.update(lst)

for number, count in counts.most_common(4):
    print number, count

Counter.most_common()方法按排序顺序为您提供条目;在这里,我们要求提供4个最常见的条目。

如果您想要只有4个数字的列表,请使用:

top4 = [n for n, c in counts.most_common(4)]

您也可以只连接输入列表,但最好是使用itertools.chain来表示:

from collection import Counter
from itertools import chain

counts = Counter(chain(a1, a2, a3, a4))

并且不会创建一个完整的列表对象,您只能再次丢弃。

演示:

>>> from collections import Counter
>>> a1 = [1,2,2,2,3,4,]
>>> a2 = [3,5,6,7,8,8,]
>>> a3 = [3,4,7,8,9,9,]
>>> a4 = [2,3,5,7,8,9,]
>>> counts = Counter(a1)
>>> for lst in (a2, a3, a4):
...     counts.update(lst)
... 
>>> for number, count in counts.most_common(4):
...     print number, count
... 
2 4
3 4
8 4
7 3
>>> [n for n, c in counts.most_common(4)]
[2, 3, 8, 7]