min_number_of_tops = 3
c = Counter(['x','b','c','d','e','x','b','c','d','x','b'])
print(c.most_common(min_number_of_tops))输出:
[('b', 3), ('x', 3), ('c', 2)] 或者:
[('b', 3), ('x', 3), ('d', 2)] 但我更希望most_common返回的内容是这样的:
[('b', 3), ('x', 3), ('d', 2), ('c', 2)]因为我感兴趣的是包含具有给定计数的所有元素。
无论如何,我需要生成一个表示前几个结果的排序列表,但也要返回与第三个项目具有相同计数的任何其他项目。例如:
['x', 'b', 'c', 'd']以下是我生成此列表的尝试:
def elements_above_cutoff(elem_value_pairs, cutoff):
    ''' presuming that the pairs are sorted from highest value to lowest '''
    for e,v in elem_value_pairs:
        if v >= cutoff:
            yield e
        else:
            return
min_number_of_tops = 3
c = Counter(['x','b','c','d','e','x','b','c','d','x','b'])
print(list(elements_above_cutoff(c.most_common(),c.most_common(min_number_of_tops)[-1][1])))这就给出了:
['b', 'x', 'd', 'c']你能推荐一种更好的方法吗?我使用的是python3。
发布于 2015-11-19 06:02:08
这个简单的方法是可行的:
import collections
common = collections.Counter(['x','b','c','d','e','x','b','c','d','x','b']).most_common()
min_number_of_tops = 3
if len(common)>min_number_of_tops:
    freq = common[min_number_of_tops-1][1]
    for i in range(min_number_of_tops, len(common)):
        if common[i][1] < freq:
            common = common[:i]
            break
print(common)   输出:
[('b', 3), ('x', 3), ('d', 2), ('c', 2)]使用列表理解的一种稍微更精细的方法可能可读性较差:
import collections
common = collections.Counter(['x','b','c','d','e','x','b','c','d','x','b']).most_common()
min_number_of_tops = 3
testfreq = common[min_number_of_tops-1][1] if len(common)>min_number_of_tops else 0
common = [x for x, freq in common if freq >= testfreq]
print(common) 输出:
['b', 'x', 'd', 'c']https://stackoverflow.com/questions/33791057
复制相似问题