如何从值为列表的键中删除一个值?

26 浏览
0 Comments

如何从值为列表的键中删除一个值?

这个问题已经有了答案:

列表中 del、remove 和 pop 的区别

这真的只是我的问题。我在网上只能找到如何删除键值对,而不是如何从一个键的值列表中删除特定值。

例如:一个键为 Emotions 的值为 [\'neutral\',\'happy\'],我只想删除 \'happy\'。dictionary[Emotions].remove(\'happy\')根本不起作用。

admin 更改状态以发布 2023年5月22日
0
0 Comments

我不确定遇到了哪些问题。您可以将您的代码与下面正确执行的代码进行比较:

Emotions = 'emotions'
dictionary = {Emotions: ['neutral', 'happy']}
print(dictionary)
dictionary[Emotions].remove('happy')
print(dictionary)

输出

{'emotions': ['neutral', 'happy']}
{'emotions': ['neutral']}

0