用python实现英文字母和相应序数转换的方法
作者:marsxu626 时间:2023-11-20 13:30:10
第一步:字母转数字
英文字母转对应数字相对简单,可以在命令行输入一行需要转换的英文字母,然后对每一个字母在整个字母表中匹配,并返回相应的位数,然后累加这些位数即可。过程中,为了使结果更有可读性,输出相邻数字间怎加了空格,每个对应原来单词间增加逗号。
c="abcdefghijklmnopqrstuvwxyz"
temp=''
list=[]
s=input()
num=len(s)
list.append(s)
for i in range(0,num):
if list[0][i]==' ':
temp+=','
else:
for r in range(1,26):
if list[0][i]==c[int(r)-1]:
temp+=str(r)
temp+=' '
print("输出结果为:%s"%temp)
第二步:数字转字母
数字转字母有个难点就是,当输入一行数字,如何才能合理地把它们每个相应位的数取出来。
才开始想到用正则匹配,定模式单元(\d+,{0,}),然后希望每个数字用.groups()形式返回一个元组(tuple),但限于要输入数字的个数位置,没找到好的匹配方式。
然后用到了split()函数,用相应的分隔符分割一段字符串之后,将值已list形式返回。
c="abcdefghijklmnopqrstuvwxyz"
temp=''
s=input()
s_list=s.split(",")
num=len(s_list)
for i in range(0,num):
if s_list[i]==' ':
temp+=' '
else:
result=c[int(s_list[i])-1]
temp+=result
print("输出结果是:%s"%temp)
完整代码
#-*- coding: utf-8 -*-
import re
def main():
ss=input("请选择:\n1.字母->数字\
\n2.数字->字母\n")
if ss=='1':
print("请输入字母: ")
fun1()
elif ss=='2':
print("请输入数字:")
fun2()
def fun1():
c="abcdefghijklmnopqrstuvwxyz"
temp=''
list=[]
s=input()
num=len(s)
list.append(s)
for i in range(0,num):
if list[0][i]==' ':
temp+=','
else:
for r in range(1,26):
if list[0][i]==c[int(r)-1]:
temp+=str(r)
temp+=' '
print("输出结果为:%s"%temp)
def fun2():
c="abcdefghijklmnopqrstuvwxyz"
temp=''
s=input()
s_list=s.split(",")
num=len(s_list)
for i in range(0,num):
if s_list[i]==' ':
temp+=' '
else:
result=c[int(s_list[i])-1]
temp+=result
print("输出结果是:%s"%temp)
if __name__ == '__main__':
main()
便可利用该python代码实现英文字母和对应数字的相互转换。
来源:https://blog.csdn.net/weixin_43952190/article/details/100921146
标签:python,英文字母,相应序数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中下划线的使用方法
2021-10-05 23:27:12
Dreamweaver4探谜系列(2)
2010-05-13 12:08:00
vue.js整合vux中的上拉加载下拉刷新实例教程
2024-04-30 10:30:17
![](https://img.aspxhome.com/file/2023/9/130169_0s.gif)
window10下mysql 8.0.20 安装配置方法图文教程
2024-01-14 19:08:59
![](https://img.aspxhome.com/file/2023/0/121280_0s.jpg)
Python使用MapReduce编程模型统计销量
2021-07-16 14:24:43
![](https://img.aspxhome.com/file/2023/8/102788_0s.png)
Python异常对代码运行性能的影响实例解析
2023-10-18 23:35:39
![](https://img.aspxhome.com/file/2023/8/87488_0s.png)
Favicon图标小常识
2010-04-01 12:01:00
Python+Opencv答题卡识别用例详解
2021-09-08 22:16:13
![](https://img.aspxhome.com/file/2023/9/64619_0s.jpg)
七种PHP开发环境搭建工具
2023-10-25 13:47:48
![](https://img.aspxhome.com/file/2023/6/55816_0s.jpg)
MsSql 存储过程分页代码 [收集多篇]
2024-01-13 13:13:33
Python正则表达式中的量词符号与组问题小结
2022-12-14 15:24:24
![](https://img.aspxhome.com/file/2023/3/64673_0s.jpg)
pip安装库报错[notice] A new release of pip available: 22.2 -> 22.2.2
2021-07-15 15:57:04
![](https://img.aspxhome.com/file/2023/8/135358_0s.jpg)
Windows下通过DOS命令登录MYSQL的方法
2024-01-27 13:21:02
python列表排序用 sort()和sorted()的区别
2021-11-27 10:36:20
浅谈javascript中的DOM方法
2024-04-29 13:44:12
go mod详细使用教程
2024-04-26 17:32:19
![](https://img.aspxhome.com/file/2023/8/133988_0s.jpg)
利用Pytorch实现获取特征图的方法详解
2023-09-11 16:16:02
python 实现IP子网计算
2022-04-24 20:27:37
pytorch中的hook机制register_forward_hook
2022-03-24 10:37:12
Python实现的多线程端口扫描工具分享
2023-03-30 04:32:19
![](https://img.aspxhome.com/file/2023/8/84888_0s.jpg)