Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何从字典中删除元素并将其作为列表返回?_Python_List_Dictionary - Fatal编程技术网

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

Python:如何从字典中删除元素并将其作为列表返回?,python,list,dictionary,Python,List,Dictionary,我一直在尝试一个练习,要求我删除字典中包含7个或更少字符的单词。到目前为止,我已经尝试使用.values()方法仅获取方括号内的单词,从而创建一个列表来存储这些单词。然而,我在使用pop方法时遇到了一个问题 “TypeError:'str'对象不能解释为整数” 这是我的密码: word_dict = {'show': ['display', 'exhibit', 'convey', 'communicate', 'manifest', 'disclose'], 'slo

我一直在尝试一个练习,要求我删除字典中包含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()
word_dict={'show':['display','exhibit','transfer','communicate','manifest','expose'],
‘慢’:[‘从容不迫’、‘渐进’、‘悠闲’、‘迟到’、‘落后’、‘乏味’、‘松弛’]
def main():
编辑的同义词=删除单词(单词)
打印(已编辑的同义词)
def删除单词(单词dict):
同义词列表=单词目录值()
新列表=[]
对于同义词列表中的i:
新列表。扩展(i)
对于新列表中的单词:
字母长度=len(单词)

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

从文档中:

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
中添加
索引
值所需的任何方法

修改代码:

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

列表上
可以根据索引弹出元素

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]

也可以通过以下方式使用列表的“删除”属性:-

for word in new_list:
    print(word)
    letter_length = len(word)
    if letter_length <= 7:
        new_list.remove(word)
新列表中的单词的
:
打印(word)
字母长度=len(单词)

如果字母长度您应该使用
remove
功能,而不是
pop

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

pop()
函数接受单个参数(索引)并删除该索引中存在的项

更多详情:

word_dict={'show':['display'、'exhibit'、'transfer'、'communicate'、'manifest'、'expose'],
‘慢’:[‘从容不迫’、‘渐进’、‘悠闲’、‘迟到’、‘落后’、‘乏味’、‘松弛’]
res=[]
对于我,在word中:
tmp=[j表示单词[i]中的j,如果len(j)
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))
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']