I'm using Python 3. I have 3 groups and in these groups I have multiple values.
I have the value that I search and I want to get his group. For example if I have CCC
I need to get GROUP 1
and if I have HHH
I want the GROUP 3
then do something according to the group.
So I think I gonna create a dict like this (tell me if I'm wrong) :
{
'group1': {'AAA','BBB','CCC','DDD'},
'group2': {'EEE','FFF','GGG'},
'group3': {'HHH','JJJ'}
}
So I see that we can revert the dict to get the key from a value so I thought to do this :
dict = {
'group1': {'AAA','BBB','CCC','DDD'},
'group2': {'EEE','FFF','GGG'},
'group3': {'HHH','JJJ'}
}
revdict = dict([(dict[key],key) for key in dict])
group = revdict['CCC']
if group == 'group1':
# Do something
elif group == 'group2':
# Do something
elif group == 'group3':
# Do something
But I don't think it's the good way to do what I want. There is a way to do something like this :
if 'CCC' in dict :
# Then get the current key. How ?
Or maybe I don't need to create dict but another things ? I open for all your suggestions.