从具有多个值的字典中删除值 [英] remove a value from a dictionary with multiple values
本文介绍了从具有多个值的字典中删除值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试从一个字典中的多个值中删除一个特定的项目,而且我完全不知道该怎么做。例如,在字典中:
I have been trying to remove a specific item from multiple values in a dictionary and I am not completely sure how to do this. For example, in the dictionary:
d = {('hello', 'hi', 'how are you'): 2, ('hi', 'hello', 'how are you'): 1}
我可以删除让所有剩下的都是:
How can I remove 'hi' so that all that remains is:
d = {('hello', 'how are you'): 2, ('hello', 'how are you'): 1}
推荐答案
这里不会得到预期的输出,因为这两个键现在相同。因此,只有其中一个可以在最终的dict中找到。
You won't get the expected output here, as both keys are now same. So, only one of them can be found in the resulting dict.
In [142]: d = {('hello', 'hi', 'how are you'): 2, ('hi', 'hello', 'how are you'): 1}
In [143]: {tuple(y for y in x if y!='hi'):d[x] for x in d}
Out[143]: {('hello', 'how are you'): 1}
这篇关于从具有多个值的字典中删除值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文