Reputation: 55
I was wondering if it's possible to loop a list of values
Example:
lst = ['RH', 'CD241', 'C2', 'SCZD9', 'RG59L', 'WNT3A']
through the values of a dictionary
Example:
ref_dict = {
'': [''], '6005': ['RH50A', 'CD241', 'SLC42A1'], '603': [''],
'6000': [''], '8787': ['PERRS', 'RGS9L', 'MGC26458'],
'41': ['ACCN2', 'BNaC2', 'hBNaC2'], '8490': [''],
'9628': [''], '5999': ['SCZD9']
}
To check if the individual value in the list has the value in the dictionary, if it does have the value, then it would return me the key in which the value is in.
Example :
lst value CD241 is in the dictionary '6005': ['RH50A, CD241, SLC42A1']
, it would return me key "6005"
.
Upvotes: 0
Views: 104
Reputation: 12108
Something like,
for key in ref_dict.keys():
if set(lst) & set(ref_dict[key]):
#do something with your key
#key is the key you want
If there are multiple keys where one of the elements in lst
will exist, then you can get the list of these keys with a list comprehension,
[key for key in ref_dict.keys() if set(lst) & set(ref_dict[key])]
which outputs ['6005', '5999']
for your case.
The magic is happening in the set intersection part,
(set(['RH', 'CD241', 'C2', 'SCZD9', 'RG59L', 'WNT3A']) &
set(['RH50A', 'CD241', 'SLC42A1']))
will give you - ['CD241']
, as good as checking if something in lst
exists in the value list or not.
Upvotes: 1
Reputation: 694
from collections import defaultdict
lst = ['RH', 'CD241', 'C2', 'SCZD9', 'RG59L', 'WNT3A']
ref_dict = {
'': [''], '6005': ['RH50A, CD241, SLC42A1'], '603': [''],
'6000': [''], '8787': ['PERRS, RGS9L, MGC26458'],
'41': ['ACCN2, BNaC2, hBNaC2'], '8490': [''],
'9628': [''], '5999': ['SCZD9']
}
all_values = defaultdict(list)
for key in ref_dict:
for value in (map(lambda x: x.strip(), ref_dict[key][0].split(","))):
all_values[value].append(key)
print all_values['CD241'] # ['6005']
Upvotes: 0
Reputation: 13
Try this:
for key in ref_dict:
if ref_dict[key] != 0:
return key
#if you want to use the value
Upvotes: 0