简体   繁体   English

python 字典中的多个键和值

[英]multiple keys and values in python dictionary

I am making Exam app with kivy(python) and I have problem with getting correct answer.我正在使用 kivy(python) 制作考试应用程序,但我无法获得正确答案。 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.所以问题是当 2 或 3 个拉丁词与 1 个斯洛文尼亚词的意思相同时,反之亦然。 Exemple:示例:

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

For example:例如:

Exemple_pic Example_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.在您看到应用程序的图像上,我必须翻译“Agito”什么意思“stresam”所以我的问题是如何检查它的多个键是否它的值是什么。

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.首先,您必须能够从图片中显示的应用程序中获取文本 output,然后使用字典进行检查。

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.您应该这样设计:key 只是一个字符串,而 values 是一个列表。 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' .因此,现在从应用程序中获取文本后,假设它是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.如果 then 的每个查找形式为Dic[latin] -> slovenian ,其中latin是一个单词, slovenian是一个或多个单词。

you could use dict.items() ( dict.iteritems() for python2, but why am I even mentioning that?)你可以使用dict.items()dict.iteritems()用于 python2,但我为什么还要提到呢?)

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.如果您想双向搜索,在 memory 使用与搜索速度之间进行权衡,您可以考虑构建第二个反向字典。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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