synonyms_dict = {'beautiful' : ['pretty', 'lovely', 'handsome', 'dazzling', 'splendid', 'magnificent']}
synonyms_dict = {k:[v1 for v1 in v if len(v1) >= 7] for k, v in synonyms_dict.items()}
print(synonyms_dict)
# {'beautiful': ['handsome', 'dazzling', 'splendid', 'magnificent']}
synonyms_dict = {'beautiful' : ['pretty', 'lovely', 'handsome', 'dazzling', 'splendid', 'magnificent']}
new_list = []
for key,value in synonyms_dict.items():
for i in range(len(value)):
if len(value[i]) >= 7:
new_list.append(value[i])
synonyms_dict['beautiful'] = new_list
print(synonyms_dict)
我认为你的问题的标题应该是从列表中删除值,而不是dict。在
可以使用remove、del或pop删除python列表中的元素。 Difference between del, remove and pop on lists
或者用一种更像Python的方式,我认为
利用听写理解和列表理解。在
假设您有
python>=3.x
,对于初学者来说,更具可读性的解决方案是:相关问题 更多 >
编程相关推荐