如何从字典中删除指定长度字符以下的值?

How do I remove values that are under a specified length of characters from a dictionary?

在此代码中,我试图从字典中删除列表中不超过 7 个字符的值(同义词)。出于某种原因,我的代码只是部分删除了 7 个或更少字符的值。另外,请不要删除任何功能或使用导入和设置来解决并尽可能保持当前代码完整。

我当前的输出:

{'show': ['exhibit', 'note', 'point to', 'indicate', 'reveal', 'demonstrate'], 'slow': ['unhurried', 'leisurely', 'behind', 'slack'], 
'dangerous': ['perilous', 'hazardous', 'uncertain']}

期望的输出:

{'show' : ['demonstrate', 'indicate', 'point to'], 
'slow' : ['leisurely', 'unhurried'],
'dangerous' : ['hazardous', 'perilous', 'uncertain']}
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)
    print(edited_synonyms)

def remove_word(word_dict):
    for key, value in word_dict.items():
        for item in value:
            if len(item) <= 7:
                value.remove(item)
    return word_dict

main()

您正在修改您在 for item in value: 时迭代的同一个列表。
相反,您需要迭代 value[:] 其中 returns 数组的副本

word_dict = {'show': ['exhibit', 'note', 'point to', 'indicate', 'reveal', 'demonstrate'], 'slow': ['unhurried', 'leisurely', 'behind', 'slack'],
'dangerous': ['perilous', 'hazardous', 'uncertain']}

def main():
    edited_synonyms = remove_word(word_dict)
    print(edited_synonyms)

def remove_word(word_dict):
    for key, value in word_dict.items():
        #Iterate on copy of value
        for item in value[:]:
            if len(item) <= 7:
                value.remove(item)
    return word_dict

main()

输出将是

{
'show': ['point to', 'indicate', 'demonstrate'], 
'slow': ['unhurried', 'leisurely'],
 'dangerous': ['perilous', 'hazardous', 'uncertain']
}

另一种选择是创建一个新列表,将带有len>7的单词添加到列表中并将列表分配给字典的键

 word_dict = {'show': ['exhibit', 'note', 'point to', 'indicate', 'reveal', 'demonstrate'], 'slow': ['unhurried', 'leisurely', 'behind', 'slack'],
'dangerous': ['perilous', 'hazardous', 'uncertain']}

def main():
    edited_synonyms = remove_word(word_dict)
    print(edited_synonyms)


def remove_word(word_dict):
    for key, value in word_dict.items():

        #List of holding words with len more that 7
        li = []
        for item in value:
            #Add words with len more than 7 to the list
            if len(item) > 7:
                li.append(item)
        #Assign the list to the key
        word_dict[key] = li
    return word_dict

main()

这可以使用 dict 和 list comp 来完成:

edited_synonyms = {k: [w for w in v if len(w) >= 7] for k, v in word_dict.items()}

我建议您使用它,而不是在遍历集合时尝试修改它。它也比现有的其他选项更有效。

创建一个新列表来过滤长度 > 7 的值,然后将其分配给相应的键。
简单地说,您可以像这样修改您的代码:

word_dict = {'show': ['exhibit', 'note', 'point to', 'indicate', 'reveal', 'demonstrate'], 'slow': ['unhurried', 'leisurely', 'behind', 'slack'],
            'dangerous': ['perilous', 'hazardous', 'uncertain']}

def main():
    edited_synonyms = remove_word(word_dict)
    print(edited_synonyms)

def remove_word(word_dict):
    for key, value in word_dict.items():
        new_val = []
        for item in value:
            if len(item) > 7:
                new_val.append(item)
        word_dict[key] = new_val
    return word_dict

main()