简体   繁体   中英

multiple keys and values in python dictionary

I am making Exam app with kivy(python) and I have problem with getting correct answer. I have dictonary of translates from latin words to slovenian words exemple(Keys are latin words, values are slovenian words):

Dic = {"Aegrotus": "bolnik", "Aether": "eter"}

So the problem is when 2 or 3 latin words mean same as 1 sloveian word and vice versa. Exemple:

Dic = {("A", "ab"): "od", "Acutus": ("Akuten", "Akutna", "Akutno"), "Aromaticus": ("Dišeč", "Odišavljen")}

For example:

Exemple_pic

On image you see app, I have to translate "Agito" what means "stresam" So my question is how to check if its multiple keys what is its value.

I hope you understand my question:).

Firstly, you have to be able to get the text output from the app shown in the picture, then you use your dictionary to check it.

And the way to design the dictionary makes it difficult to check. You should design it that way: key is only one string, and values is a list. For example:

Dic = {"A": ["od"], "ab": ["od"], "Acutus": ["Akuten", "Akutna", "Akutno"], "Aromaticus": ["Dišeč", "Odišavljen"]}

So now after you get the text from your app, let's say it is text = 'ab:id' . You will split it to key and value then check in your dict:

def check(text):
    text = text.split(':')
    key = text[0]
    value = text[1]
    if value in Dic[key]:
        return True
    return False

Let's try it out

>>> check('ab:id')
False
>>> check('ab:od')
True
>>> check('Acutus:Akutna')
True
>>> check('Acutus:Akutno')
True

Do you only need to translate from latin -> slovenian and not the other way around? If so, just make every key a single word. It's OK for multiple keys to have the same value:

Dic = {
    "Aegrotus": "bolnik", "Aether": "eter", "A": "od", "ab": "od",
    "Acutus": ("Akuten", "Akutna", "Akutno"), "Aromaticus": ("Dišeč", "Odišavljen"),
}

Each lookup if then of the form Dic[latin] -> slovenian , where latin is a single word and slovenian is one or more words.

you could use dict.items() ( dict.iteritems() for python2, but why am I even mentioning that?)

so try something like

for latin_words, slovenian_words in dic.items():
    if isinstance(latin_words, tuple):
        # this is the check
        # if there are multiple values
        # this will run
        ...

    if isinstance(slovenian_words, tuple):
        # this is the check
        # if there are multiple values
        # this will run
        ...

If you want to search both ways, at the trade off of memory usage vs speed of searching, you could consider building a second reversed dictionary. I changed your example to have unique Latin keys in the first dictionary, and then create a second dictionary which has a slightly different structure (can't add to tuples, so sets are used instead), but should be searchable in the same manner as the first.

from collections import defaultdict

Dic = {"A": "od", "ab": "od", "Acutus": ("Akuten", "Akutna", "Akutno"), "Aromaticus": {"Dišeč", "Odišavljen"}}

Dic2 = defaultdict(set)
for k, v in Dic.items():
    if isinstance(v, str):   # just one translation
        Dic2[v].add(k)
    else:                    # more than one translation, given as a tuple
        for i in v:
            Dic2[i].add(k)

#print(Dic)
#print(Dic2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM