Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正在删除计数器中计数小于阈值的对象。_Python - Fatal编程技术网

Python 正在删除计数器中计数小于阈值的对象。

Python 正在删除计数器中计数小于阈值的对象。,python,Python,我将计数器声明为:main\u dict=counter(),并将值添加为main\u dict[word]+=1。最后,我想删除所有频率小于15的元素。计数器中是否有执行此操作的功能 感谢您的帮助。否,您需要手动删除它们。使用itertools.dropwhile()可能会让这更容易一些: from itertools import dropwhile for key, count in dropwhile(lambda key_count: key_count[1] >= 15, m

我将计数器声明为:
main\u dict=counter()
,并将值添加为
main\u dict[word]+=1
。最后,我想删除所有频率小于15的元素。
计数器中是否有执行此操作的功能


感谢您的帮助。

否,您需要手动删除它们。使用
itertools.dropwhile()
可能会让这更容易一些:

from itertools import dropwhile

for key, count in dropwhile(lambda key_count: key_count[1] >= 15, main_dict.most_common()):
    del main_dict[key]
演示:

>>> main_dict
Counter({'baz': 20, 'bar': 15, 'foo': 10})
>>> for key, count in dropwhile(lambda key_count: key_count[1] >= 15, main_dict.most_common()):
...     del main_dict[key]
... 
>>> main_dict
Counter({'baz': 20, 'bar': 15})
使用
dropwhile
只需测试计数为15或以上的键;之后,它将放弃测试,只需通过所有测试。这在排序的
最常见()列表中非常有效。如果有很多值低于15,则可以节省所有这些测试的执行时间。

另一种方法:

c = Counter({'baz': 20, 'bar': 15, 'foo': 10})
print Counter(el for el in c.elements() if c[el] >= 15)
# Counter({'baz': 20, 'bar': 15})

我可以提出另一个解决办法吗

from collections import Counter
main_dict = Counter({'baz': 20, 'bar': 15, 'foo': 10})  
trsh = 15

main_dict = Counter(dict(filter(lambda x: x[1] >= trsh, main_dict.items())))
print(main_dict)

>>> Counter({'baz': 20, 'bar': 15})
我也有同样的问题,但我需要从计数器返回一个值超过某个阈值的所有键的列表。这样做

keys_list = map(lambda x: x[0], filter(lambda x: x[1] >= trsh, main_dict.items()))
print(keys_list) 

>>> ['baz', 'bar']

阈值为零时的优雅解决方案:

main_dict += Counter()

如何筛选计数器中计数大于或小于阈值的项的示例

from collections import Counter
from itertools import takewhile, dropwhile


data = (
    "Here's a little song about Roy G. Biv. "
    "He makes up all the colors that you see where you live. "
    "If you know all the colors, sing them with me: "
    "red, orange, yellow, green, blue, indigo, violet all that you see."
)

c = Counter(data)

more_than_10 = dict(takewhile(lambda i: i[1] > 10, c.most_common()))
less_than_2 = dict(dropwhile(lambda i: i[1] >= 2, c.most_common()))

print(f"> 10 {more_than_10} \n2 < {less_than_2}")
从集合导入计数器
从itertools导入takewhile、dropwhile
数据=(
“这里有一首关于罗伊·G·比夫的小歌。”
“在你居住的地方,你所看到的所有颜色都是由他创造的。”
“如果你知道所有的颜色,就和我一起唱吧:”
“红、橙、黄、绿、蓝、靛、紫——你所看到的一切。”
)
c=计数器(数据)
多于10=dict(takewhile(lambda i:i[1]>10,c.最常见())
小于等于dict(dropwhile(lambda i:i[1]>=2,c.最常见())
打印(f“>10{大于10}\n2<{小于2}”)
输出:

> 10 {' ': 40, 'e': 23, 'o': 16, 'l': 15, 't': 12} 
2 < {"'": 1, 'R': 1, 'G': 1, 'B': 1, 'p': 1, 'I': 1, 'f': 1, ':': 1}
>10{':40,'e':23,'o':16,'l':15,'t':12}
2<{“':1,'R':1,'G':1,'B':1,'p':1,'I':1,'f':1,:':1}

只需列出字典项:

[el for el in c.items() if el[1] >= 15]

此外,不应删除
“bar”
,因为其频率不低于15。实际上,这个想法可能以不同的方式工作,但实际上并不适用于删除密钥。例如,
new_dict=dict(takewhile(lambda x:x[1]>=15,main_dict.most_common())
@jamylak:
dropwhile
一旦停止匹配就停止测试;对于大量的关键点,这会产生影响。我稍后会调查错误。@MartijnPieters,但这并不重要,因为您已经将整个
.most_common()
列表存储到内存中first@jamylak当前位置由于我们正在修改字典,您至少必须将所有键存储在内存中。修复了错误。@MartijnPieters您仍然必须遍历每一个键,但为什么
.elements()
超过
.items()
,后者将是faster@jamylak因为
.items
返回一个元组,当传回
计数器时
得到一个值为
1的键/值对元组(我想-责怪深夜和酒吧午餐)好吧,或者你也可以这样做:
计数器({k:c代表k,c.items()中的c如果c>=15})
在Python3中,我得到了“RuntimeError:迭代过程中更改了字典大小”@Ferran你是对的,更新为Py2/Py3兼容
计数器
is
dict
用于计数,当两个
计数器
相加时,如果它们的值为0,结果的键也会被删除。我不会在代码中使用此技巧,但它很优雅。
[el for el in c.items() if el[1] >= 15]