[Python Challenge] - Q1

本文详细解析了一个Python挑战中的字符映射问题,通过实例展示了如何使用string.maketrans()方法实现字符串转换,并提供了相应的代码示例。此外,文章还介绍了该方法的使用场景及其在处理长文本转换时的效率优势。

Q1:What about making trans?everybody thinks twice before solving this.

 

http://www.pythonchallenge.com/pc/def/map.html

 

题目的意思是将给定的字符串按照图中"K-M O-Q E-G"的方式替代,于是有了下面的方法:

 

代码1:

 

输出结果:

i   h o p e   y o u   d i d n t   t r a n s l a t e   i t   b y   h a n d .   t h a t s   w h a t   c o m p u t e r s   a r e   f o r .   d o i n g   i t   i n   b y   h a n d   i s   i n e f f i c i e n t   a n d   t h a t ' s   w h y   t h i s   t e x t   i s   s o   l o n g .   u s i n g   s t r i n g . m a k e t r a n s ( )   i s   r e c o m m e n d e d .   n o w   a p p l y   o n   t h e   u r l .  

 

看输出结果,作者建议了string.maketrans()方法,查了一下,又有了下面的代码:

 

代码2:

 

输出结果:

"i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url. "

 

过关的话将网址中的map按照规则替换为ocr即可。

 

另学习一下string.maketrans()方法(我理解的):

此方法返回一个table,用以指示intab和outtab两个字符串对应的替代位置,注意intab和outtab应具有相同长度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值