简体   繁体   English

如何在 Python 中指定带重音的字母与不带重音的字母相同?

[英]How to assign a letter with an accent to mean the same to a letter without the accent in Python?

Im trying to make it so for example, ķ==k, but im having trouble finding a way that dosent replace the "ķ" to "k".我试图做到这一点,例如,ķ==k,但我无法找到一种将“ķ”替换为“k”的方法。 Essentially i want them to mean the same thing.本质上我希望他们的意思是一样的。 Im making a hangman game and since its in Latvian, I have been given a task to make the letters that the user will guess/input mean the same as a letter without accent.我正在制作一个刽子手游戏,因为它是拉脱维亚语,所以我被赋予了一项任务,让用户猜测/输入的字母与没有重音的字母意思相同。

minejums = input("Ievadi minējamo burtu: ") minejums = input("Ievadi minējamo burtu: ")

latviesuburti = {"ā": "a", "č": "c", "ē": "e", "ģ": "g", "ī": "i", "ķ": "k", "ļ": "l", "ņ": "n", "š": "s", "ū": "u", "ž": "z"} latviesuburti = {"ā": "a", "č": "c", "ē": "e", "ģ": "g", "ī": "i", "ķ": "k" , "ļ": "l", "ņ": "n", "š": "s", "ū": "u", "ž": "z"}

My idea so far was to use a dictionary.到目前为止,我的想法是使用字典。 I will use a for loop, but i dont know what function to use.我将使用 for 循环,但我不知道要使用什么 function。 Please help.请帮忙。

Since you don't want to actually change the letters in the puzzle, I would define a function that uses the dict to map each letter onto the bare Latin equivalent, and just always compare letters by passing both of them through the function first.由于您不想实际更改拼图中的字母,因此我会定义一个 function,它使用 map 的字典将每个字母转换为对应的裸拉丁字母,并且总是通过首先将两个字母传递给 function 来比较字母。

latviesuburti = {"ā": "a", "č": "c", "ē": "e", "ģ": "g", "ī": "i", 
                 "ķ": "k", "ļ": "l", "ņ": "n", "š": "s", "ū": "u", 
                 "ž": "z"}

def canonicalize(letter):         
    return latviesuburti.get(letter, letter)

...
for letter in puzzle:
    if canonicalize(guess) == canonicalize(letter):
        ... handle a correct guess ...

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

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