Python: How do I remove elements from a dictionary

2020-07-26 02:08发布

I have been trying an exercise that requires me to remove words in a dictionary containing words that are 7 or fewer characters long. So far I have tried using the .values() method to get only the words within the square brackets, and thus create a list to store these words. However, I am having a problem with using the pop method where I get

"TypeError: 'str' object cannot be interpreted as an integer".

Here is my code :

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

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

def remove_word(word_dict):
    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(word)

    return new_list

main()

8条回答
虎瘦雄心在
2楼-- · 2020-07-26 02:26

Try this :

outp = {k: [v for v in word_dict[k] if len(v)<=7 ]for k in word_dict}

Output :

{'show': ['communicate', 'manifest', 'disclose'], 'slow': ['unhurried', 'leisurely']}
查看更多
孤傲高冷的网名
3楼-- · 2020-07-26 02:27

On a list you can pop elements based on the index.

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

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

def remove_word(word_dict):
    synonyms_list = word_dict.values()
    new_list = list()
    for i in synonyms_list:
        new_list.extend([word for word in i if len(word) > 7])
    return new_list

main()
查看更多
Lonely孤独者°
4楼-- · 2020-07-26 02:30

Personally I would do it like this:

for key in word_dict.keys():
    word_dict[key] = [x for x in word_dict[key] if len(x)>7]
查看更多
虎瘦雄心在
5楼-- · 2020-07-26 02:31

Your program is right. Any ways you need to add index value in pop.

Modified code:

>>> def remove_word(word_dict):
    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)) # Modified line

    return new_list

Output:

>>> main()
['exhibit', 'communicate', 'manifest', 'disclose', 'unhurried', 'leisurely', 'behind', 'slack']
查看更多
甜甜的少女心
6楼-- · 2020-07-26 02:31
word_dict = {'show': ['display', 'exhibit', 'convey', 'communicate', 'manifest', 'disclose'],
                 'slow': ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious', 'slack']}


res =[]

for i in word_dict:
    tmp =[j for j in word_dict[i] if len(j)<=7]
    res.extend(tmp)

print(res)

# output ['display', 'exhibit', 'convey', 'gradual', 'late', 'behind', 'tedious', 'slack']
查看更多
放荡不羁爱自由
7楼-- · 2020-07-26 02:36

Or you can use the remove property of list in the below way:-

for word in new_list:
    print(word)
    letter_length = len(word)
    if letter_length <= 7:
        new_list.remove(word)

Alternate way:-

print(new_list)
for word in new_list:
    print(word)
    letter_length = len(word)
    if letter_length <= 7:
        new_list.pop(new_list.index(word))
查看更多
登录 后发表回答