Python 如何从包含6个或更多字母的字典关键字列表中删除单词?

Python 如何从包含6个或更多字母的字典关键字列表中删除单词?,python,Python,我不知道如何创建一个函数,该函数能够从作为字典键值的每个列表中删除少于6个字符的单词 我试图弹出列表中少于6的每个单词,但得到的是“TypeError:无法解压缩不可iterable int对象”。我不知道我使用的方法是否正确 def remove_word(words_dict): items_list = list(words_dict.items()) for key, value in range(len(items_list) -1, -1, -1):

我不知道如何创建一个函数,该函数能够从作为字典键值的每个列表中删除少于6个字符的单词

我试图弹出列表中少于6的每个单词,但得到的是“TypeError:无法解压缩不可iterable int对象”。我不知道我使用的方法是否正确

def remove_word(words_dict):
    items_list = list(words_dict.items())

    for key, value in range(len(items_list) -1, -1, -1):
        if len(value) < 6:
            items_list.pop()
    words_dict = items_list.sort()
    return words_dict
应打印:

1.
colours : []
places : ['america', 'malaysia', 'argentina']
animals : ['monkey']

可以使用嵌套循环执行此操作:

for key in words_dict:
    words_dict[key] = [i for i in dict[key] if len(i) >= 6]
循环理解(基于前一个列表中的条件构建新列表)实际上是完成此任务的最简单方法,因为python是如何处理列表迭代器的。实际上,你也可以把它放在听写理解中:

new_words_dict = {key: [i for i in value if len(i) >= 6] for key, value in words_dict.items()}

您可以使用dict上的循环和嵌套理解来实现这一点

words_dict = {
    'colours' : ['red', 'blue', 'green'],
    'places' : ['america', 'china', 'malaysia', 'argentina', 'india'],
    'animals' : ['lion', 'cat', 'dog', 'wolf', 'monkey','zebra'],
}

for key, lst in words_dict.items():
    filtered_lst = [word for word in lst if len(word) >= 6]
    print(f"{key} : {filtered_lst}")
其输出为:

colours : []
places : ['america', 'malaysia', 'argentina']
animals : ['monkey']
或者,要实际生成一个函数,该函数本质上删除元素并返回更正后的dict,正如您的代码最初所做的那样,请使用以下内容:

def remove_words(words_dict):
    return {key: [word for word in lst if len(word) >= 6] 
            for key, lst in words_dict.items()}
但是,您仍然需要在它们上循环以正确打印

words_dict = remove_words(words_dict)
for key, lst in words_dict.items():
    print(f"{key} : {lst}")

{k:[i for i in v if len(i)>5]k,v in words_dict.items()}

也许不是最干净的方法,这不是一种有效的方法,但它是可读的,我这样写的,所以你可以看到它工作的逻辑

 In [23]: def remove_word(my_dict):
    ...:     for key in my_dict:
    ...:         to_delete = []
    ...:         for values in my_dict[key]:
    ...:             if len(values) < 6:
    ...:                 to_delete.append(values)
    ...:         for word in to_delete:
    ...:             my_dict[key].remove(word)
    ...:     return my_dict
    ...:
    ...:
输出

    {'colours': [], 'places': ['america', 'malaysia', 'argentina'], 'animals': []}

这行不通,您需要使用
单词dict.items()
并且OP正在弹出项目
<6
,因此
len(i)
需要
=6
>5
才能遵循其原始规定。@Jab感谢您的更正。据此编辑。
In [26]: remove_word(words_dict)
Out[26]:
{'colours': [],
 'places': ['america', 'malaysia', 'argentina'],
 'animals': ['monkey']}
# input data
words_dict = {'colours' : ['red', 'blue', 'green'],
    'places' : ['america', 'china', 'malaysia', 'argentina', 'india'],
    'animals' : ['lion', 'cat', 'dog', 'wolf', 'monkey',
                'zebra'],
    }
# creating a final output dictionary 

#looping through each key value pair present in dictionary and adding the key 
# the final dictionary and processed valeus to the corresponding key
# using lambda function, fast readable and easy to understand 
result = {k:list(filter(lambda x:len(x)>=6, v)) for k,v in words_dict.items()}
print(result)
    {'colours': [], 'places': ['america', 'malaysia', 'argentina'], 'animals': []}