【问题标题】:python lists/dictionaries with probabilities具有概率的python列表/字典
【发布时间】:2019-01-20 05:51:54
【问题描述】:

大家早上好 :) 我目前正在做一个词汇培训师。 我有一本字典,其中存储了所有词汇及其翻译。现在我有一个查询,告诉我应该翻译哪些词汇。

如果我现在正确输入翻译,那么被查询的单词的概率应该会降低。我怎样才能做到这一点?我想知道,在回答正确的翻译时,是否可以通过制作另一个应该比第一个更少被调用的列表并将词汇表移动到该列表中来实现这一点。

这是我的代码:

import random

vokabeln = {
    "Haus": "house",
    "Garten": "garden",
    "Freund": "friend",
    "Freundin": "friend"
}

versuche = int(input("Anzahl der Versuche: "))
i=0

while i < versuche:
    x = random.choice(list(vokabeln))
    y = vokabeln.get(x)

    i+=1
    versuch = input("Übersetze " + x)
    if(versuch == y):
        print("Korrekt!")
    else:
        print("Falsch, richtig war " + y)

【问题讨论】:

  • 请将代码翻译成英文
  • 第 18 行 versuch = input("Übersetze " + x) 也不起作用
  • 你想要的是一个加权选择,其中权重与正确答案计数成反比。
  • 因为您正在尝试添加“Übersetze” + x。 x 是一个整数。你得到这个错误 UnicodeEncodeError: 'ascii' codec can't encode character '\xdc' in position 0: ordinal not in range(128)
  • @TalhaIsrar x 不是整数,而是字符串。您收到的错误与其他问题有关,也许您的控制台不支持 Unicode 字符(“Ü”)。

标签: python python-3.x list dictionary


【解决方案1】:

您可以执行以下操作。每次用户正确翻译时,将该单词添加到单独的列表中。下次随机选择一个单词并且它在新列表中时,允许它以一定的概率使用,比如 50%;否则,请选择另一个词。你需要把这个逻辑放在它自己的循环中,以防随机选择另一个“正确”的词。

import random

vokabeln = {
    "Haus": "house",
    "Garten": "garden",
    "Freund": "friend",
    "Freundin": "friend"
}

korrekt = []

versuche = int(input("Anzahl der Versuche: "))
i=0

while i < versuche:
    ok = False
    while not ok:
        x = random.choice(list(vokabeln))
        y = vokabeln.get(x)
        if x in korrekt:
            if random.random() < 0.5:
                ok = True
        else:
            ok = True

    i+=1
    versuch = input("Übersetze " + x)
    if(versuch == y):
        korrekt.append(x)
        print("Korrekt!")
    else:
        print("Falsch, richtig war " + y)

【讨论】:

  • 是的,但我怎样才能添加这个概率?并感谢您的回答。 :)
  • @JohnKohlmeier 我已经添加了我描述的代码。一个快速测试表明,一个正确的单词被重复使用的概率实际上应该小于 50%,也许是 10%。当然,这取决于您的喜好。
【解决方案2】:

快速的想法。

words = {
    ("Haus", "house", 1),
    ("Garten", "garden", 0.5),
    ("Freund", "friend", 1),
    ("Freundin", "friend", 1)
}

def get_word():
    total_probability = sum(map(words, lambda x: x[2]))
    selected = random.random() * total_probability
    current_probability = 0
    for word, translation, probability in words:
        current_probability += probability
        if select < current_probability:
            return word, translation

【讨论】:

    【解决方案3】:

    您可以使用random.choices,它允许指定样本权重。然后我还将词汇表存储为一个列表,以保持权重的排序。如何根据正确或错误答案更新权重取决于您,但您可以使用反向缩放,例如:

    vocab = [
        ("Haus", "house"),
        ("Garten", "garden"),
        ("Freund", "friend"),
        ("Freundin", "friend")
    ]
    weights = [1] * len(vocab)
    
    while ...:
        index, (x, y) = random.choices(enumerate(vocab), weights)
    
        attempt = input("Translate " + x)
    
        if(attempt == y):
            weights[index] = 1 / (1/weights[index] + 1)
        else:
            weights[index] = 1 / max(1/weights[index] - 1, 1)
    

    【讨论】:

      猜你喜欢
      • 2016-09-12
      • 2021-02-16
      • 1970-01-01
      • 2013-08-19
      • 2015-06-08
      • 2011-05-15
      • 2021-07-01
      • 1970-01-01
      • 2018-07-20
      相关资源
      最近更新 更多