从具有多个值的字典中删除值

问题描述:

我一直在尝试从一个字典中的多个值中删除一个特定的项目,而且我完全不知道该怎么做。例如,在字典中:

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}