【发布时间】:2021-06-14 12:12:28
【问题描述】:
这是返回值中包含“10”的字典键的代码。
word_freq = {'is': [1, 3, 4, 8, 10],
'at': [3, 10, 15, 7, 9],
'test': [5, 3, 7, 8, 1],
'this': [2, 3, 5, 6, 11],
'why': [10, 3, 9, 8, 12]}
# Check if a value exist in dictionary with multiple value
value = 10
# Get list of keys that contains the given value
list_of_keys = [key
for key, list_of_values in word_freq.items()
if value in list_of_values]
if list_of_keys:
print(list_of_keys)
else:
print('Value does not exist in the dictionary')
输出:['is', 'at', 'why']
我想修改它以查找多个值,例如“10”和“3”,并返回值中包含这些数字的键。
请提出解决方案
【问题讨论】:
-
看
or命令。
标签: python list dictionary key-value