如何浏览字典以更新并返回字典?

时间:2019-05-25 12:56:27

标签: python list dictionary for-loop

在此代码中,字典word_dict中任何7个字符或更少字符的值(同义词)都将从字典中删除。

我设法删除了不超过7个字符的值,最初,我尝试创建一个新字典来存储更新后的值,但是没有成功,我弄乱了我的代码。

关于如何无需创建新词典就可以编辑现有词典值的任何想法?我宁愿不必使用集合,理解力或单线来完成此任务。预期的输出应该是这样的:(可以是任意顺序)

{
    'show' : ['communicate', 'manifest', 'disclose'],
    'dangerous' : ['hazardous', 'perilous', 'uncertain'],
    'slow' : ['leisurely', 'unhurried'],
}

我尝试用来解决该问题的代码主要位于remove_word(word_dict)函数之内。

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) #should print out an edited dictionary

def remove_word(word_dict):
    dictionary = {}

    synonyms_list = word_dict.values()
    new_list = []
    for i in synonyms_list:
        new_list.append(i)

    for word in new_list:
        letter_length = len(word)
        if letter_length <= 7:
            new_list.pop(new_list.index(word))

    return dictionary

3 个答案:

答案 0 :(得分:3)

您可以只更新字典中每个键的每个列表:

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

编辑:实际上,您不必创建新的temp列表,可以使用remove:

for key, value in word_dict.items():
    for item in value:
        if len(item) > 7:
            value.remove(item)

答案 1 :(得分:1)

您可以像下面这样从现有列表中过滤掉值。


from pprint import pprint

word_dict = {'show': ['display', 'exhibit', 'convey', 'communicate', 'manifest', 'disclose'],
             'slow': ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious', 'slack'],
             'dangerous': ['perilous', 'hazardous', 'uncertain']}

for k, v in word_dict.items():
    word_dict[k] = list(filter(lambda x: len(x) > 7, v))

pprint(word_dict)


输出:

{'dangerous': ['perilous', 'hazardous', 'uncertain'],
 'show': ['communicate', 'manifest', 'disclose'],
 'slow': ['unhurried', 'leisurely']}

答案 2 :(得分:0)

有两种方法可以做到这一点:

  1. 您可以简单地使用相同的变量构建字典,并使用列表理解来填充每个键的值。

    from pprint import pprint
    
    word_dict = {'show': ['display', 'exhibit', 'convey', 'communicate', 'manifest', 'disclose'],
                 'slow': ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious', 'slack'],
                 'dangerous': ['perilous', 'hazardous', 'uncertain']}
    
    for key_word in word_dict:    # loops through each identifier word
        word_dict[key_word] = [   # list comprehensioin
                word
                for word in word_dict[key_word] # fetch the original words list 
                if len(word) > 7  # fetches the list of words of size gt 7
        ]
    
    pprint(word_dict)
    
  2. 使用纯字典理解

    word_dict = {key_word: [word for word in word_dict[key_word] if len(word) > 7] for key_word in word_dict}
    

两者都会导致

{'dangerous': ['perilous', 'hazardous', 'uncertain'],
 'show': ['communicate', 'manifest', 'disclose'],
 'slow': ['unhurried', 'leisurely']}