繁体   English   中英

Python:如何从字典中删除元素并将其作为列表返回?

[英]Python: How do I remove elements from a dictionary and return it as a list?

我一直在尝试一项练习,要求我删除字典中包含7个或更少字符长的单词的单词。 到目前为止,我已尝试使用.values()方法仅获取方括号内的单词,从而创建一个列表来存储这些单词。 但是,我遇到了使用pop方法的问题

“TypeError:'str'对象不能解释为整数”。

这是我的代码:

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()

list.pop将要从列表中弹出的项目的索引作为参数,但是当您执行new_list.pop(word) ,实际上是在提供项目本身,因此错误。

来自文档: https//docs.python.org/3/tutorial/datastructures.html

list.pop([I])
删除列表中给定位置的项目,然后将其返回。 您可以使用字典理解轻松解决此问题,您只需在列表中包含长度> 7的值

要解决此问题,只需找到要通过list.remove()弹出的单词的索引。

所以行

 new_list.pop(word)

将改为

 new_list.remove(word)

但更好的方法可能是使用像这样的列表理解

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

#Iterate through the values of the list and pick one with length > 7
res = [v for value in word_dict.values() for v in value if len(v) > 7  ]
print(res)

输出将是

['communicate', 'manifest', 'disclose', 'unhurried', 'leisurely']

你的计划是对的。 您需要在pop添加index值的任何方法。

修改后的代码

>>> 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

输出:

>>> main()
['exhibit', 'communicate', 'manifest', 'disclose', 'unhurried', 'leisurely', 'behind', 'slack']

尝试这个 :

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

输出

{'show': ['communicate', 'manifest', 'disclose'], 'slow': ['unhurried', 'leisurely']}

list您可以根据索引弹出元素。

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()

就个人而言,我会这样做:

for key in word_dict.keys():
    word_dict[key] = [x for x in word_dict[key] if len(x)>7]

或者您可以通过以下方式使用list的remove属性: -

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

替代方式: -

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))

你应该使用remove功能而不是pop

remove()函数将单个元素作为参数。

pop()函数接受一个参数(索引)并删除该索引处的项目。

更多细节 :

https://docs.python.org/3/tutorial/datastructures.html

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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM