我的代码应该删除字典中长度不超过7个字符的同义词,一旦我完成了所有删除,我将需要按键顺序显示它。但是,我不知道如何将键和更新值放在一起。预期的输出应与以下内容完全相同:
(按字母顺序排序的键):
dangerous : ['hazardous', 'perilous', 'uncertain']
show : ['communicate', 'manifest', 'disclose']
slow : ['leisurely', 'unhurried']
word_dict = {'show': ['display', 'exhibit', 'convey', 'communicate', 'manifest', 'disclose'],
'slow': ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious', 'slack'],
'dangerous': ['perilous', 'hazardous', 'uncertain']}
def main():
edited_synonyms = remove_word(word_dict)
key_order(edited_synonyms)
def remove_word(word_dict):
dictionary = {}
synonyms_list = word_dict.values()
new_list = []
for i in synonyms_list:
new_list.extend(i)
for word in new_list:
letter_length = len(word)
if letter_length <= 7:
new_list.pop(new_list.index(word))
value = new_list
keys_only = word_dict.keys()
key = keys_only
dictionary[key] = value
return dictionary
def key_order(word_dict):
word_list = list(word_dict.keys())
word_list.sort()
for letter in word_list:
value = word_list[letter]
print(letter, ": ", value)
main()
到目前为止发生的一个错误是"TypeError: unhashable type:'dict_keys'“
https://stackoverflow.com/questions/56258595
复制相似问题