2

I'm not sure if a dictionary is the way to go, but I made a dictionary that contains a list of 1-7 items. I would like to access the key given any of the items in the list. Is this possible? I found a way to access a key given one value (Get key by value in dictionary), but what if there are multiple values? My end goal is to repeat a string (String A) given a separate string, (String B) that is categorized under String A. For example, 'Category A' contains aa, bb, cc and 'Cateogry B' contains a, b, c. A list of [aa, a, bb, aa, c, b, aa] would equal to: [A, B, A, A, B, B, A]. This is my attempt so far, however it does not work.

dict = {'A':['aa', 'bb', 'cc'],'B':['a','b','c'] }
nwlst = []
lst = [aa, a, bb, aa, c, b, aa]
for i in lst:
    if i in dict.values():
        nwlst.append([list(dict.keys())[list(dict.values()).index([str(i)])]])

Perhaps using something like grep could work too but I don't know how to implement this concept.

Spencer Trinh
  • 743
  • 12
  • 31

3 Answers3

1

Not sure your end goal but a dictionary wouldn't be used for this kind of case.

Generally "looking up by value" is not how you want to use a dictionary. If it really is the case you should be flipping your keys and values so that you end up using the value as the key.

dict = {
  'aa': 'A',
  'bb': 'A',
  'cc': 'A',
  'a': 'B',
  'b': 'B',
  'c': 'B',
}

That way you can get it correctly thus if you MUST go from value to key look up then given the use case it is more likely that you take your original dictionary and transform it into this version of the dictionary for lookup.

dict = {'A':['aa', 'bb', 'cc'],'B':['a','b','c'] }
values_to_keys_dict = {}
for key, values in dict:
  for value in values:
    values_to_keys_dict[value] = key

lst = ['aa', 'a', 'bb', 'aa', 'c', 'b', 'aa']
for i in lst:
  values_to_keys_dict[i] # To get the "key"

This way you only scan all your values once instead of on each value in lst you scan all the values. Now, this has a caveat, if your "values" set has same values such as { 'A': ['a'], 'B': ['a'] } then this would "lose" information that multiple keys have same value.

richardhsu
  • 878
  • 5
  • 13
1

I guess you need something very similar to the accepted answer in the question you linked, but instead of testing for equality you would test for inclusion in list. Plus, you forgot to use quotes for items in lst.

dict = {'A':['aa', 'bb', 'cc'],'B':['a','b','c'] }
newlst = []
lst = ['aa', 'a', 'bb', 'aa', 'c', 'b', 'aa']
for search_v in lst:
    for k, v in dict.items():
        if search_v in v:
            newlst.append(k)

There are other options as well, related to other answers in the linked question.

For instance, you can reverse the dictionary as in this answer. You can even do it with a one liner (not tetsted)

dict = {'A':['aa', 'bb', 'cc'],'B':['a','b','c'] }
new_dict = {iv: k for k, v in dict.items() for iv in v}

and then

newlst = []
lst = ['aa', 'a', 'bb', 'aa', 'c', 'b', 'aa']
for search_k in lst:
    newlst.append(new_dict[search_k])

There are one liners for this too.

PS: searching python reverse dictionary in Google gives you a lot of options. See in particular this.

0

Consider the Disjoint Set data structure. After [ 'aa', 'bb', 'cc' ] and ['a', 'b', 'c'] are made into disjoint subsets using the Union method, the Find method would return the respective parent objects.

The structure can be implemented with sets, lists, dictionaries, etc. Here is a fairly good one, with other more/less complicate implementations being easily found on the web.

lo__on
  • 1
  • 1