小编Jua*_*ruz的帖子

搜索列表中的值是否在格式为key-string的字典中,value-list(字符串)

my_dict = {                              # This dictionary is generated thru
'a' : [ 'value1', 'value4', 'value5' ],  # the info given by the user
'b' : [ 'value2', 'value6', 'value7'],
'c' : [ 'value3', 'value8', 'value9']
}

list = [ 'value1', 'value2' ] # List is generated using list comprehension
Run Code Online (Sandbox Code Playgroud)

我需要生成一个列表,输出如下所示:

output_list = ['a', 'b']
Run Code Online (Sandbox Code Playgroud)

我需要检查"list"上的值是否与字典内列表中的值匹配.这甚至可能吗?

我尝试使用它,但我只得到一个空列表:

[key for key, value in my_dict.items() if value in list]
Run Code Online (Sandbox Code Playgroud)

python dictionary list python-3.x

5
推荐指数
1
解决办法
56
查看次数

标签 统计

dictionary ×1

list ×1

python ×1

python-3.x ×1