0

say i have a dictionary like this:

{'etova ER400': 'rack1', 'enzomac forte': 'rack1', 'myosone': 'rack1', 'etova 200': 'rack2', 'etogesic ER': 'rack2'}

and Now,if my searchQuery says 'eto'; am getting the related VALUE in the output

output

Q: But how to get the list of all the keys for that particular VALUE as well say if the searchQuery says 'enzomac', OUTPUT: enzomac forte ---> rack1 ; and rack1 -> ['etova ER400', 'enzomac forte','myosone']

i tried this:

rev_dict = {}
flipped={}
myDict = {}

def get_key(val):
if val not in flipped:
    flipped[val] = [key]
else:
    flipped[val].append(key)
return(flipped)

myDict.update(dict.fromkeys(['etova ER400', 'enzomac forte','myosone'], 'rack1'))
myDict.update(dict.fromkeys(['etova 200','etogesic ER'], 'rack2'))
search_key = input("Enter name of med : ").lower()
for key, val in myDict.items():
    if key.startswith(search_key):
        print(key, " ---> ", val)
    rev_dict=get_key(val)
print(rev_dict)
DYZ
  • 55,249
  • 10
  • 64
  • 93
swagato
  • 3
  • 2
  • 1
    Does this answer your question? [Get key by value in dictionary](https://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary) – Alex K. Aug 30 '20 at 03:27

1 Answers1

0

I dont know if I understood correctly what you wanted with the rev_dict it seems its just the complete reverse dict, which makes it weird for me to be processed in a query because the query result is not needed for the reverse dict. However here you go:

reverse_dict = {}
rev_dict = {}
flipped={}
myDict = {}

myDict.update(dict.fromkeys(['etova ER400', 'enzomac forte','myosone'], 'rack1'))
myDict.update(dict.fromkeys(['etova 200','etogesic ER'], 'rack2'))
search_key = input("Enter name of med : ").lower()
lowest_key = None
for key in myDict.keys():
    item = myDict[key]
    if key.startswith(search_key):
        print(key + "--->" + item)
        keyvalue = int(item[4:])
        if lowest_key is None or lowest_key > keyvalue:
            lowest_key = keyvalue
    if item not in reverse_dict.keys():
        reverse_dict[item] = []
    reverse_dict[item].append(key)

if lowest_key is not None:
    rack = 'rack' + str(lowest_key)
    rev_dict = {rack: reverse_dict[rack]}
    print(rev_dict)
seven_seas
  • 703
  • 4
  • 21
  • Thanks for the response , but what i tried to ask is-- if any medicine is in rack1, the output rev_dict content should display the list of ONLY Rack1 medicines(not of Rack2). eg: search_key= 'enzomac', OUTPUT: enzomac forte ---> rack1 ; and rack1 -> ['etova ER400', 'enzomac forte','myosone'] – swagato Aug 31 '20 at 04:40
  • just wanted to let you know that 'rack1,'rack2' was just an example, in case if we have text like 'pain relievers - LH(B) -> 4th ROW extreme left' , 'antiallergy - small box -LH(B) -> 1st ROW extreme right', then it will be difficult to do it using your method of populating that KEYVALUE field – swagato Aug 31 '20 at 10:43
  • @swagato absolutely, however in that case i would need to know in what order you want to determine which rack gets outputted in the `rev_dict`? – seven_seas Aug 31 '20 at 15:14