简体   繁体   English

法语Python字典

[英]French Dictionary on Python

Essentially what I wanted my code to do is to have a user input a word. 本质上,我希望代码执行的操作是让用户输入一个单词。 The code would then search through the dictionary (French_Dictionary), attempt to find that word and if successful, print the translation of that word. 然后,该代码将搜索字典(French_Dictionary),尝试查找该单词,如果成功,则打印该单词的翻译。

I am a beginner to python, and I was just wondering whether anyone could help me understand at the very least the concepts needed to code these steps. 我是python的初学者,我只是想知道是否有人可以帮助我至少理解编写这些步骤所需的概念。

This is my code so far (essentially just formalities) 到目前为止,这是我的代码(基本上只是形式)

def opening_statement():
    print "Welcome to a French Dictionary"
    while True:
        print "Choose [1] (English to French)   or   [2] (French to English) "
        choice = input("Enter 1 or 2 ")
        if choice == 1:
                    print "You have selected the English to French Dictionary"
                    break
        elif choice == 2:
            print "You have selected the French to English Dictionary"
            break
        else:
            print "n/a"

def secondary_statement():
    while True:
        word = raw_input("Type your word: ")
        word = word.lower()
        print 'Please confirm - Your word is "%s"' % (word)
        answer= input("Yes [1]   or   No [2] ")
        if answer == 1:
            print "Translating..."
            break
        elif answer == 2:
            print "Repeating process..."
        else:
            print "n/a"

#Will add more and more words manually to dictionary
French_Dictionary = {
"aller" : "to go",
"avoir" : "to have"
}

print opening_statement()
print secondary_statement()

To give you the high level idea: 为了给您高层次的想法:

You use the english word as key in a translations dictionary to get the french version of the word: 您将英语单词用作翻译词典中的键来获取单词的法语版本:

>>> dictionary_of_words = {"yes":"oui", "no":"non"}
>>> dictionary_of_words["yes"]
'oui'
>>>

So, when you read a word as input, check if it is contained in the list of keys of the dictionary, and if it is, print the translation, else throw an exception for example. 因此,当您读取一个单词作为输入时,请检查该单词是否包含在字典的键列表中,如果包含,则打印翻译,否则抛出异常。 To iterate over a dictionary, looping over the keys also works: 要遍历字典,遍历键也可以:

>>> list_keys = dictionary_of_words.keys()
>>> list_keys
['yes', 'no']
>>> for i in list_keys:
...     print dictionary_of_words[i]
... 
oui
non
>>> 

While I appreciate you're using a dictionary data structure for your dictionayr; 我很感谢您为字典使用字典数据结构; if you're going to translate both ways, perhaps a different data structure is more appropriate? 如果您要同时翻译这两种方法,那么也许更合适的是不同的数据结构? Something like 2 lists where you can link them via the indexes or something? 像2列表一样,您可以在其中通过索引或其他内容链接它们?

from functools import partial

eng = ["yes", "no"]

fr = ["oui", "non"]

def freng(fr, eng, mot):
    return eng[fr.index(mot)]

def engfr(fr, eng, word):
    return fr[eng.index(word)]

freng = partial(freng, fr, eng)
engfr = partial(engfr, fr, eng)

The partial function simplifies the calling signature so you can just have freng("oui") and get back "yes". 局部函数简化了调用签名,因此您只需拥有freng(“ oui”)并返回“ yes”即可。

Notice the handling of cases where a word isn't in the dictionary; 注意在字典中没有单词的情况下的处理。 as it stands that would throw an exception. 就目前而言,这将引发异常。 You may want to handle that more gracefully. 您可能需要更优雅地处理。 Good luck! 祝好运!

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

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