接收字母的Python函数,返回字母中的(从0开始)数字位置 [英] Python Function That Receives Letter, Returns (0-Based) Numerical Position Within Alphabet

查看:217
本文介绍了接收字母的Python函数,返回字母中的(从0开始)数字位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Python函数,该函数接收一个字母(一个只有一个字母字符的字符串),并返回该字母在字母中从0开始的数字位置。它不应该区分大小写,并且我不能使用import。

I'm trying to create a Python function that receives a letter (a string with only one alphabetic character) and returns the 0-based numerical position of that letter in the alphabet. It should not be case-sensitive, and I can't use import.

因此输入 a应返回

0

输入 A也应返回

0

输入 O应返回

14

以此类推。

我注意到了这个问题,但是最上面的答案使用导入,第二个答案对我来说没有任何意义/不起作用。我试图应用这样的第二个答案:

I had noticed this question but the top answer uses import and the second answer doesn't make any sense to me / doesn't work. I tried to apply the second answer like this:

letter = input("enter a letter")

def alphabet_position(letter):

    return ord(letter) - 97

print((alphabet_position)(letter))

但是我遇到TypeError:

but I got a TypeError:

TypeError: ord() expected a character, but string of length 2 found

就像问题中的提问者一样链接的同时,我也试图将字母 x的步数发送回字母,但是要做到这一点,我需要首先创建此辅助函数。

Just like the asker in the question that I linked, I'm also trying to send the characters "x" amount of steps back in the alphabet, but in order to do that I need to create this helper function first.

我想必须要有一种方法将字母存储在两个单独的列表中,一个小写字母和一个大写字母,然后查看字符串是否输入的用户与该列表中的项目之一匹配?然后,找到匹配项后,我们返回它的(从0开始)数字位置?

I'm thinking there must be a way to store the letters in two separate lists, one lower-case and one upper-case, and then see if the string that the user entered matches one of the items in that list? Then once we find the match, we return it's (0-based) numerical position?

letter = input("enter a letter")

def alphabet_position(letter):

    position = 0
    #letter_position = index value that matches input
    lower_case_list ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
 'y', 'z'] 

    upper_case_list ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
 'Y','Z']              

#if letter is in lower_case_list, return it's 0-based numerical position.
#else, if letter is in upper_case_list, return it's 0-based numerical position.
#else, print("Enter a valid letter")

return letter_position

如有任何建议,请提供帮助。谢谢。

Please help if you have any suggestions. Thank you.

推荐答案

我建议使用字典。做相对简单的事情可能需要大量代码,但是我发现以这种方式理解它更容易(如果您不熟悉python,它将帮助您学习)。如果您使用google python字典,则可以了解更多有关它们的信息,但这是基本概念:

I suggest using a dictionary. It might be a large amount of code to do something relatively simple, but I find it easier to make sense of it this way (and if you are new to python it will help you learn). If you google python dictionaries you can learn lots more about them, but here is the basic concept:

python 3.X版本的代码:

Code for python version 3.X:

def alphabet_position(letter):
    alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1}
    pos = alphabet_pos[letter]
    return pos
letter = input('Enter a letter: ')
print(alphabet_position(letter))

python 2.7版的代码:

Code for python version 2.7:

def alphabet_position(letter):
    alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1}
    pos = alphabet_pos[letter]
    return pos
letter = raw_input('Enter a letter: ')
print alphabet_position(letter)

如果运行该命令,它将打印 1 ,因为它会搜索 alpahbet_pos 字典,并找到与标题为 B的条目相对应的值。请注意,可以有多个具有相同值的条目,因此可以在同一字典中使用大写和小写形式。为了节省时间,我只做字母A和B,所以您可以自己填写其余的内容;)

If you run that, it will print 1 because it searches through the alpahbet_pos dictionary and finds the value that corresponds to the entry entitled 'B'. Notice that you can have multiple entries with the same value, so you can do uppercase and lowercase in the same dictionary. I only did letters A and B for the sake of time, so you can fill out the rest yourself ;)

我曾经不得不输入周期表中的每个元素,它们相应的原子质量,并且永远存在(感觉比实际要长得多)。

I once had to enter every element on the periodic table and their corresponding atomic mass, and that took forever (felt much longer than it actually was).

这篇关于接收字母的Python函数,返回字母中的(从0开始)数字位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆