例如:
synonyms_dict = {'look': ['gaze', 'see', 'glance', 'watch', 'peruse'],
'put': ['place', 'set', 'attach', 'keep', 'save', 'set aside',
'effect',
'achieve', 'do', 'build'],
'beautiful': ['pretty', 'lovely', 'handsome', 'dazzling', 'splendid',
'magnificent'],
'slow': ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious',
'slack'],
'dangerous': ['perilous', 'hazardous', 'uncertain']}
remove_short_synonyms(synonyms_dict)
print("1.")
print_dict_in_key_order(synonyms_dict)
synonyms_dict = {'come': ['approach', 'advance', 'near', 'arrive', 'reach'],
'show': ['display', 'exhibit', 'present', 'point to', 'indicate', 'explain',
'prove', 'demonstrate', 'expose'],
'good': ['excellent', 'fine', 'superior', 'wonderful', 'grand', 'superb',
'edifying'],
'bad': ['evil', 'immoral', 'wicked', 'contaminated', 'spoiled', 'defective',
'substandard', 'faulty', 'improper', 'inappropriate']}
remove_short_synonyms(synonyms_dict)
print("2.")
print_dict_in_key_order(synonyms_dict)
我试图从每个对应的同义词列表中删除所有少于7个字符的同义词,并且该函数对每个对应的同义词列表进行排序。我试过这个功能。
def remove_short_synonyms(synonyms_dict):
dicts = list(synonyms_dict.keys())
for word in sorted(dicts):
if len(word) <= 7:
del(synonyms_dict[word])
预期:
beautiful : ['dazzling', 'handsome', 'magnificent', 'splendid']
dangerous : ['hazardous', 'perilous', 'uncertain']
look : []
put : ['achieve', 'set aside']
slow : ['gradual', 'leisurely', 'tedious', 'unhurried']
2.
bad : ['contaminated', 'defective', 'immoral', 'improper', 'inappropriate',
'spoiled', 'substandard']
come : ['advance', 'approach']
good : ['edifying', 'excellent', 'superior', 'wonderful']
show : ['demonstrate', 'display', 'exhibit', 'explain', 'indicate', 'point
to', 'present']
答案 0 :(得分:1)
不要从原始文件中删除,只需使用字典理解重建过滤后的版本,对值进行最大尺寸过滤,然后输入synonyms_dict = {'look': ['gaze', 'see', 'glance', 'watch', 'peruse'],
'put': ['place', 'set', 'attach', 'keep', 'save', 'set aside',
'effect',
'achieve', 'do', 'build'],
'beautiful': ['pretty', 'lovely', 'handsome', 'dazzling', 'splendid',
'magnificent'],
'slow': ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious',
'slack'],
'dangerous': ['perilous', 'hazardous', 'uncertain']}
def remove_short_synonyms(s):
return {k:sorted([i for i in v if len(i)>7]) for k,v in s.items()}
print(remove_short_synonyms(synonyms_dict))
对值列表进行排序:
{'beautiful': ['dazzling', 'handsome', 'magnificent', 'splendid'], 'look': [], 'dangerous': ['hazardous', 'perilous', 'uncertain'], 'slow': ['leisurely', 'unhurried'], 'put': ['set aside']}
结果:
kubectl