I have a dictionary that has list of strings as values:
components = {
'skin': [
'first',
'second',
'third',
],
'hair': [
'fourth',
'fifth',
]
}
I want to return the key if a matching string was found in all of the lists, i.e.:
fourth
-> returns hair
or
second
-> returns skin
etc.
CodePudding user response:
Try this:
>>> def fnd_key(dct, srch_wrd):
... for k,v in dct.items():
... if srch_wrd in v:
... return k
>>> components = {'skin': ['first', 'second', 'third'], 'hair': ['fourth','fifth']}
>>> fnd_key(components, 'second')
'skin'
>>> fnd_key(components, 'fourth')
'hair'
You can try this if value
(like:second) exist in multi list
:
>>> def fnd_key(dct, srch_wrd):
... for k,v in dct.items():
... if srch_wrd in v:
... yield k
>>> components = {'skin': ['first', 'second', 'third'],
'hair': ['fourth','fifth', 'second']}
>>> for ky in fnd_key(components, 'second'):
... print(ky)
skin
hair
CodePudding user response:
Use the in keyword.
for key in components.keys():
if search in components[key]:
print(key)
Beware that, the time complexity of in depends on the container.
- list - Average: O(n)
- set/dict - Average: O(1), Worst: O(n)
CodePudding user response:
print([k for k,v in components.items() if 'fourth' in v][0])
CodePudding user response:
You could write a search algorithm but repeating the search many times is inefficient. If your values in the sublists are unique, you should reverse and flatten your dictionary.
>>> components_rev = {v:k for k, values in components.items() for v in values}
>>> components_rev
{'first': 'skin', 'second': 'skin', 'third': 'skin', 'fourth': 'hair', 'fifth': 'hair'}
>>> components_rev['fourth']
'hair'
CodePudding user response:
Try this
components = {
'skin': [
'first',
'second',
'third',
],
'hair': [
'fourth',
'fifth',
]
}
def find_key(myDict, value):
for key in myDict:
if value in myDict[key]:
return key
return -1
print(find_key(components, 'fourth'))
CodePudding user response:
for example you can do something like this if you wanted to make a dictionary of key value of pairs like: 'found item': 'key for found item'
but like many said you might need to find a better structure than list if you are working with really big data or something. but unless the data really is huge i doubt there will be any real problem
products = {
'electronics': [
'laptop',
'tv',
'monitor'
],
'food': [
'doritos',
'mac n cheese'
]
}
# words to look for = 'mac n cheese', 'tv'
words = ['mac n cheese', 'tv']
found_words = {}
# for every key in components ('electronics', 'food')
for product_type_key in products.keys():
# for each item in components[key] in this case list
for product in products[product_type_key]:
if product in words:
print(f"{product} found in products['{product_type_key}']")
found_words[product] = f'was found in {product_type_key}'
print('\n')
print("found_words:")
for k, v in found_words.items():
print(f"{k}: {v}")
output:
tv found in products['electronics']
mac n cheese found in products['food']
found_words:
tv: was found in electronics
mac n cheese: was found in food