【发布时间】:2021-12-19 02:09:38
【问题描述】:
我有一本看起来像这样的字典:
word_freq = {
"Hello": 56,
"at": 23,
"test": 43,
"this": 78
}
还有一个值列表list_values = [val1, val2]
我需要检查list_values 中的所有values: val1 and val2 是否作为word_freq 字典中的值存在。
我尝试用is函数解决问题:
def check_value_exist(test_dict, value1, value2):
list_values = [value1, value2]
do_exist = False
for key, value in test_dict.items():
for i in range(len(list_values)):
if value == list_values[i]:
do_exist = True
return do_exist
必须有一个简单的方法来做到这一点,但我还是 python 的新手,无法弄清楚。如果 word_freq 中的展位值无效,请尝试。
【问题讨论】:
-
exist = all( v in test_dict for v in [value1,value2] ) -
查找
all(...)。 -
您是在检查值还是键?从代码和问题文本看来是值。
标签: python dictionary