Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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_Dictionary_Search_Find - Fatal编程技术网

Python 逐字查字典

Python 逐字查字典,python,dictionary,search,find,Python,Dictionary,Search,Find,我有两本字典: v = {'name': ['Peter', 'Paul', 'Mary'], 'city': ['London', 'Rome', 'NY'], 'country': ['GB', 'I', 'USA'], 'age': ['30', '35', '45']} d = {'city': 'Rome', 'country': 'I', 'age': 35} 如果我在v中搜索d中的确切值,我希望返回Paul 我必须“嵌套循环”通过所有的可能性,还是有一个简单的方法像 if al

我有两本字典:

v = {'name': ['Peter', 'Paul', 'Mary'], 'city': ['London', 'Rome', 'NY'], 'country': ['GB', 'I', 'USA'], 'age': ['30', '35', '45']}

d = {'city': 'Rome', 'country': 'I', 'age': 35}
如果我在
v
中搜索
d
中的确切值,我希望返回
Paul

我必须“嵌套循环”通过所有的可能性,还是有一个简单的方法像

if all of d anywhere in v.rows:
    print(v[name])

循环遍历
name
元素,并将其他列表的相应元素与
d
值进行比较

found = None
for i, name in v['name']:
    if all(v[key][i] == val for key, val in d):
        found = name

假设你有相同的长度

for i in range(len(v['name'])):
    #compare all the other details and print the name
    if d['city']==v['city'][i] and d['country']==v['country'][i] \
        and d['age']==v['age'][i]:
        print(v['name'][i])

为什么要使用列表字典而不是字典列表?您当前的数据结构不适合这种搜索。