Python 3.x 如何在Python 3.4中获得字母表中的字符位置?
我需要知道文本中第n个字符的字母表位置,我阅读了of,但它不适用于我的Python 3.4Python 3.x 如何在Python 3.4中获得字母表中的字符位置?,python-3.x,alphabet,Python 3.x,Alphabet,我需要知道文本中第n个字符的字母表位置,我阅读了of,但它不适用于我的Python 3.4 我的节目 # -*- coding: utf-8 -*- """ Created on Fri Apr 22 12:24:15 2016 @author: Asus """ import string message='bonjour' string.lowercase.index('message[2]') 它不适用于ascii_小写,也不适用于小写 错误消息 runfile('C:/Use
我的节目
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 22 12:24:15 2016
@author: Asus
"""
import string
message='bonjour'
string.lowercase.index('message[2]')
它不适用于ascii_小写,也不适用于小写
错误消息 runfile('C:/Users/Asus/Desktop/Perso/WinPython-64bit-3.4.3.4/python-3.4.3.amd64/Scripts/ESSAI.py', wdir='C:/Users/Asus/Desktop/Perso/WinPython-64bit-3.4.3.4/python-3.4.3.amd64/Scripts') 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 runfile('C:/Users/Asus/Desktop/Perso/WinPython-64bit-3.4.3.4/python-3.4.3.amd64/Scripts/ESSAI.py', wdir='C:/Users/Asus/Desktop/Perso/WinPython-64bit-3.4.3.4/python-3.4.3.amd64/Scripts') 文件 “C:\Users\Asus\Desktop\Perso\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”, 第685行,在runfile中 execfile(文件名、命名空间) 文件 “C:\Users\Asus\Desktop\Perso\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”, execfile中的第85行 exec(编译(打开(文件名'rb').read(),文件名'exec'),命名空间) 文件 “C:/Users/Asus/Desktop/Perso/WinPython-64bit-3.4.3.4/python-3.4.3.amd64/Scripts/ESSAI.py”, 第11行,在 string.lowercase.index('消息') AttributeError:'module'对象没有属性'lowercase' o/p 这将适用于您,请删除更改索引中的
“
当您给出
'
时,它将被视为一个字符串 你可能在为类似的事情射击
string.ascii_lowercase.index(message[2])
返回13。您缺少ascii\uu
这将起作用(只要消息是小写的),但需要在字母表上进行线性搜索,以及导入模块
相反,只需使用
ord(message[2]) - ord('a')
另外,你可以使用
ord(message[2].lower()) - ord('a')
如果希望在消息
中的某些字母是大写字母时使用此选项
如果您希望,例如a
的排名为1而不是0,请使用
1 + ord(message[2].lower()) - ord('a')
print(message.lower().index(message[k])的答案是k我想得到字母表的排名。所以信息[2]是n,字母表中n的秩是14而不是2。。。呃:/
ord(message[2].lower()) - ord('a')
1 + ord(message[2].lower()) - ord('a')