I have a dictionary in Python with people's last names as the key and each key has multiple values linked to it. Is there a way to iterate over the dictionary using a for loop to search for a specific value and then return the key that the value is linked to?
for i in people:
if people[i] == criteria: #people is the dictionary and criteria is just a string
print dictKey #dictKey is just whatever the key is that the criteria matched element is linked to
There maybe multiple matches as well so I need to people to output multiple keys.
解决方案
You can use list comprehension
print [key
for people in peoples
for key, value in people.items()
if value == criteria]
This will print out all the keys for which the value matches the criteria. If people is the dictionary,
print [key
for key, value in people.items()
if value == criteria]