Reputation: 31
I have a dictionary of around 4000 Latin words and their English meanings. I've opened it within Python and added it to a list and it looks something like this:
[{"rex":"king"},{"ego":"I"},{"a, ab":"away from"}...]
I want the user to be able to input the Latin word they're looking for and then the program prints out the English meaning. Any ideas as to how I do this?
Upvotes: 0
Views: 35
Reputation: 168
Well you could just cycle through your 4000 items...
listr = [{"rex":"king"},{"ego":"I"},{"a, ab":"away from"}]
out = "no match found"
get = input("input please ")
for c in range(0,len(listr)):
try:
out = listr[c][get]
break
except:
pass
print(out)
Maybe you should make the existing list into multiple lists alphabetically ordered to make the search shorter. Also if the entry does not match exactly with the Latin word in the dictionary, then it won't find anything.
Upvotes: 0
Reputation: 107287
You shouldn't put them in a list. You can use a main dictionary to hold all the items in one dictionary, then simply access to the relative meanings by indexing.
If you get the tiny dictionaries from an iterable object you can create your main_dict like following:
main_dict = {}
for dictionary in iterable_of_dict:
main_dict.update(dictionary)
word = None
while word != "exit"
word = input("Please enter your word (exit for exit): ")
print(main_dict.get(word, "Sorry your word doesn't exist in dictionary!"))
Upvotes: 1