Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在列表中搜索一个值,并在python字典中找到它的键_Python_Json_Dictionary - Fatal编程技术网

在列表中搜索一个值,并在python字典中找到它的键

在列表中搜索一个值,并在python字典中找到它的键,python,json,dictionary,Python,Json,Dictionary,假设字典包含这样一个列表,我想查找字典中是否存在“name”,然后在python中查找它的键。。。。。有人能帮忙吗 test_dict = { "what is your name" :['name',"what's your name",'tell me your name ?'], "time" : ['time','whats the time'] } 因此,搜索“name”的预期输出将类似于“wha

假设字典包含这样一个列表,我想查找字典中是否存在“name”,然后在python中查找它的键。。。。。有人能帮忙吗

test_dict = { "what is your name" :['name',"what's your name",'tell me your name ?'],
              "time" : ['time','whats the time'] }
因此,搜索“name”的预期输出将类似于“what is your name”

您只需要一个,如下所示:

test_dict = { "what is your name" :['name',"what's your name",'tell me your name ?'],
              "time" : ['time','whats the time'] }

results = [k for k,v in test_dict.items() if "name" in v]

您只需要一个[理解列表][1],如下所示: (固定)


如果这个名字同时包含在两个列表中呢?它对[k代表k,v在test_dict.items()不起作用,如果v中的“nilanjan”,那么test_dict={“what is your name”:[“what's your name”,“tell me your name”,“nilanjan”],“time”:[“time',“what the time”,“back my code”:[“备份”,“git添加所有”]}
test_dict = { "what is your name" :['name',"what's your name",'tell me your name ?'],"time" : ['time','whats the time'] }
for t_d in test_dict:
    results = [k for k,v in t_d.items() if "name" in v]

print("100"*100)
print(results)
print("100"*100)