【问题标题】:Need help to make a simple dictionary in python需要帮助在 python 中制作一个简单的字典
【发布时间】:2019-12-07 01:49:16
【问题描述】:

我需要一个简单的字典,它的工作原理是这样的:首先它需要我们拥有 4 种语言翻译的单词数(n),然后我们在每一行(总共 n 行)输入我们拥有的单词及其翻译以空格分隔作为字典源,然后我们需要要求用户输入一个短语,该短语可能比以前作为字典源提供的单词多,并且该短语不仅可以是一种特定语言,例如它可能是混合英语,法语和德语,但我们需要将短语翻译成第一语言,在我们的例子中是波斯语,如果没有翻译,它只会打印单词本身。所以这里是代码:

def read_dictionary():
for i in range(0, words_num):
    dict_words = str(input())
    words[dict_words.split()[0]] = dict_words.split()[1:]


def translator():
    translation = ""
    input_phrase = str(input("Enter your phrase to translate please: ")).split()
    for word in input_phrase:
        for k, v in words.items():
            if word in v:
                translation += (k + " ")
            else:
                translation += (word + " ")
    print(translation)

words_num = int(input("Enter the number of existing translated words in dictionary: "))
words = {}
read_dictionary()
translator()

所以这里是示例输入和期望的输出:

**input:**
Enter the number of existing translated words in dictionary: 
4
man I je ich        
kheili very très sehr
alaghemand interested intéressé interessiert 
barnamenevisi programming laprogrammation Programmierung
Enter your phrase to translate please: 
I am very interested in programming
**output:**
man am kheili alaghemand in barnamenevisi

但它的输出是这样的:

man I I I am am am am very kheili very very interested interested alaghemand interested in in in in programming programming programming barnamenevisi 

【问题讨论】:

    标签: python dictionary


    【解决方案1】:

    问题源于您的translator() 函数。

    对于input_phrase 中的每个word,您将遍历words.items() 中的每个k, v 对。到目前为止还不错。

    那么我们想要发生什么?我们想要查看每对 k, v 直到找到匹配的单词(在 v 中),然后(如果我们这样做)我们想要将键 k 添加到我们正在构建的 translation 中。如果我们没有找到匹配的词,我们只需将原始的word 值添加到translation

    您的代码的问题在于,即使您确实找到了一个单词,您也会继续查看字典中剩余的键。此外,对于字典中的每个键,您要么添加它的翻译,要么添加起始 word,如果没有找到匹配项,您应该只添加基本 word

    就我个人而言,我会将您的内部 for 循环移动到一个名为 translate_word() 或类似名称的新函数中,但是在不进行重大更改的情况下修复您的 translator() 函数的简单方法如下:

    def translator():
        translation = ""
        input_phrase = str(input("Enter your phrase to translate please: ")).split()
        for word in input_phrase:
            found = False
            for k, v in words.items():
                if word in v:
                    translation += (k + " ")
                    found = True
                    break
            if not found:
                translation += (word + " ")
        print(translation)
    

    在新的translator() 函数中,您可以看到对于每个新词,您将变量found 设置为Falsefound 将代表您是否在字典中找到了合适的翻译。

    然后您将遍历字典。如果找到匹配项,则将 k 添加到翻译中,将 found 设置为 True(因为您找到了翻译),并将 break 排除在内部 for 循环之外。

    一旦退出内部 for 循环,您检查是否有 foundword 的翻译。如果没有找到翻译,只需将word 添加到translation

    【讨论】:

    • 非常感谢您的详细解释,我将把 translate_word() 方法用于内部循环,使代码更好。
    【解决方案2】:

    您可以为每种语言定义一个字典,而不是为每个单词定义一个字典(包含三种语言),例如:

    dct_eng=dict(), dct_frnch=dict() , dct_grmn=dict()
    

    每个字典的键是输入的第一个单词,这个键对于所有三个都是相同的,每个字典的值分别是第二个和第三个和第四个单词 rom 输入 现在您可以遍历每个单词并再次遍历字典中的每个值以找到对应的值键 最后使用像 Kieran Moynihan 提到的 found = False 机制来获取单词的翻译或获取单词本身,以防字典中不存在翻译。

    translation=''
    for word in sentence:# sentence is a list of words which user inputs
    found=False
    for item in words_dct_eng.keys():
        if word== words_dct_eng[item]:
            translate+=str(item)+' '
            found = True
        if word == words_dct_frch[item]:
            translate += str(item) + ' '
            found = True
        if word == words_dct_grmn[item]:
            translate += str(item) + ' '
            found=True
    if not found:
        translate += str(word) + ' '
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多