StrangeWalnut
StrangeWalnut

Reputation: 25

Dictionary value to key

I have a dictionary file with words from english language on the left and the definiton of the words in another language on the right. Something like this:

a little    xxx, xxx, xxx
a little at a time  xxx
a little too thick  xxx xxx xxx
a lot   xxx, xxx, xxx
a lot alike xxx xxx

And I have the program to read the from the file, which needs to stay like this:

fail = open("English_dictionary.txt", encoding = "UTF-8")
dictionary = {}
for row in fail:
    words = row.split("\t")
    xxx_word = words[0]
    english_word = words[1]
    dictionary[xxx_word] = english_word
fail.close()

The point is, I have to write a program, that asks the user to input a word from the other language(right side), and the program gives the definition of the xxx word in english. So basically from value to key. And the question needs to be repeatedly asked so I started it like this, but now Im stuck because I don't know how to get the key definition when I have the value.

a = 0
while a == 0:
     word = input("Insert xxx word: ")


     break

Upvotes: 1

Views: 121

Answers (1)

Sri
Sri

Reputation: 2318

Dictionaries do not work like that. They work by looking up a key to get a value. You should invert your dictionary, and then look up accordingly.

inv_map = {v: k for k, v in my_map.items()}

Edit: I am not sure if I converted the dictionary correctly. If you input 'tublisti kõvasti, palju', you will get 'a lot'

dict = {
'a little too thick': 'pisut liiga paks',
'a lot': 'tublisti kõvasti, palju',
'a lot alike': 'tublisti sarnane',
'a matter of time': 'aja küsimus',
'a million': 'miljon',
'a native of': 'pärit',
'a number of hulk': 'palju, mitmed',
'a number of times': 'korduvalt'
}

inv_dict = {v: k for k, v in dict.items()}

while (True):
    word = input("Insert xxx word:")
    if (word in inv_dict):
        print(inv_dict[word])
        break

Upvotes: 1

Related Questions