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']发布于 2021-10-02 14:16:49
不,我不认为有。只有你对函数的调用才会让你很难理解算法背后的逻辑。
例如,这段代码不那么优雅,但我希望它更容易理解;因为它不是为了提出解决方案,而只是为了让其他人更好地理解你的问题的本质。
from collections import Counter
from typing import Generator
def most_common_elements_above_cutoff(counter:Counter, cutoff:int) -> Generator[str, None, None]:
elem_value_pairs = counter.most_common()
partial_count = 0
temporary_max_occurrences = elem_value_pairs[0][1]
for (element,occurrences) in elem_value_pairs:
if partial_count < cutoff:
if occurrences != temporary_max_occurrences:
temporary_max_occurrences = occurrences
partial_count += 1
else:
if occurrences != temporary_max_occurrences:
break
yield element
min_number_of_tops = 3
c = Counter(['x','b','c','d','e','x','b','c','d','x','b'])
print(list(most_common_elements_above_cutoff(c, min_number_of_tops)))https://stackoverflow.com/questions/33791057
复制相似问题