返回列表中以给定的最小频率发生的项目

时间:2012-10-02 04:23:15

标签: python function sequence probability

需要帮助创建函数freqitems(S,p),它返回序列S中以至少p%频率出现的项目的排序列表,没有重复项。示例输出应该是这样的:

>>> freqitems([2,2,2,3],50)
[2]
>>> freqitems(5*["alpha"]+["beta"]+3*["gamma"]+7*["delta"], 25)
['alpha', 'delta']
>>> freqitems(5*["alpha"]+["beta"]+3*["gamma"]+7*["delta"], 33)
['delta']

1 个答案:

答案 0 :(得分:1)

yourlist = 5*["alpha"]+["beta"]+3*["gamma"]+7*["delta"]

def freqitems(sequence, p):
  import collections
  counter = collections.Counter(sequence)
  return sorted([k for k,v in counter.iteritems() if 100.*v/len(sequence) >= p])

print freqitems(yourlist, 25)
print freqitems(yourlist, 33)