Python 计数器保持值高于 n 次计数
Python counter keep values above n counts
我有一个单词字典和它们在给定语料库中出现的次数。我怎样才能保留至少出现 n 次的单词(比方说,n=10)?
dictionary = {
'beryllium': 60,
'inch': 56,
'any': 51,
'such': 31,
'court': 26,
'be': 25,
'by': 23,
'arsenic': 21,
'person': 20,
'land': 20,
'Lapp': 16,
'county': 15,
'Associate_in_Nursing': 15,
'executor': 15,
'information_technology': 14,
'state': 14,
'angstrom': 14,
'not': 14,
'other': 14,
'boundary': 14,
'tree': 13,
'administrator': 12,
'are': 11,
'helium': 11,
'no': 11,
'action': 10,
'rich_person': 10,
'use': 10,
'astatine': 10,
'being': 9,
'tobacco': 9,
'every': 9,
'curse': 9,
'ordain': 8,
'justice': 8,
'one': 8,
'notice': 8,
'law': 8,
'pound': 8,
'debt': 8,
'creditor': 8
}
}
对于上面的虚拟示例,它应该 return 包含一个停在 'astatine' 的字典。
从现有字典中删除(就地改变原始字典)
thresh = 10
delete = [key for key in dictionary if dictionary[key] < thresh]
# delete the key
for key in delete:
del dictionary[key]
一行:
thresh = 10
for key in [key for key in dictionary if dictionary [key] < thresh]: del dictionary[key]
filtered_dict = {key: value for key, value in count_dict.items() if value >= 10}
我有一个单词字典和它们在给定语料库中出现的次数。我怎样才能保留至少出现 n 次的单词(比方说,n=10)?
dictionary = {
'beryllium': 60,
'inch': 56,
'any': 51,
'such': 31,
'court': 26,
'be': 25,
'by': 23,
'arsenic': 21,
'person': 20,
'land': 20,
'Lapp': 16,
'county': 15,
'Associate_in_Nursing': 15,
'executor': 15,
'information_technology': 14,
'state': 14,
'angstrom': 14,
'not': 14,
'other': 14,
'boundary': 14,
'tree': 13,
'administrator': 12,
'are': 11,
'helium': 11,
'no': 11,
'action': 10,
'rich_person': 10,
'use': 10,
'astatine': 10,
'being': 9,
'tobacco': 9,
'every': 9,
'curse': 9,
'ordain': 8,
'justice': 8,
'one': 8,
'notice': 8,
'law': 8,
'pound': 8,
'debt': 8,
'creditor': 8
}
}
对于上面的虚拟示例,它应该 return 包含一个停在 'astatine' 的字典。
从现有字典中删除(就地改变原始字典)
thresh = 10
delete = [key for key in dictionary if dictionary[key] < thresh]
# delete the key
for key in delete:
del dictionary[key]
一行:
thresh = 10
for key in [key for key in dictionary if dictionary [key] < thresh]: del dictionary[key]
filtered_dict = {key: value for key, value in count_dict.items() if value >= 10}