简体   繁体   English

将小写字母更改为:数字00-25,将大写字母更改为:数字26-51

[英]Change lowercase letters to: numbers 00-25, and uppercase letters to: numbers 26-51

I have already tried a couple different methods and come away with no avail. 我已经尝试了几种不同的方法,但无济于事。 We need to perform this with simple operators and simple loops. 我们需要使用简单的运算符和简单的循环来执行此操作。 Here is what I have tried: 这是我尝试过的:

def str2num(inputstr):
    i = ''

    for x in inputstr:

        i = i + str.replace(x, str(ord(x)),1)
    i = str(i)

    print(i)

Came away with an error stating that in integer cannot be converted to a string implicitly, which I tried to fix with the str(ord(x), but I still received the same error. 出现了一个错误,指出无法将整数整数隐式转换为字符串,我尝试使用str(ord(x)进行修复,但仍然收到相同的错误。

Then I decided I would try to convert to a list and use the import string...things got messy: 然后我决定我将尝试转换为列表并使用导入字符串...事情变得混乱了:

def str2num(s):

    import string

    r = string.ascii_lowercase + string.ascii_uppercase
    L = list(r)

    for i in L:
        L [i] = 00 + 1

    print(L)

I am out of solutions that I can come up with, any suggestions? 我没有可以提出的解决方案,有什么建议吗? Remember (and I know it is a pain, but) simple solutions only. 记住(我知道这很痛苦,但是)仅是简单的解决方案。 Use for, with, while, if, import string, and general mathematical operators. 用于导入字符串和常规数学运算符。 This class is not about efficiency, but using the basic tools to get the job done. 此类与效率无关,而是使用基本工具来完成工作的。

Thank you. 谢谢。

To get a list of numbers: 获取数字列表:

In [172]: t='abcABC'

In [175]: l = map(lambda x: ord(x)-97 if x.islower() else ord(x)-65+26, t)

In [176]: l
Out[176]: [0, 1, 2, 26, 27, 28]

To get a string with 1 to '01' : 要获得1'01'的字符串:

In [179]: ''.join('%02d'%i for i in l)
Out[179]: '000102262728'

function version: 功能版本:

In [186]: def str2num(s):
     ...:   res=[]
     ...:   for i in s:
     ...:       if i.islower():
     ...:           res.append(ord(i)-97)
     ...:       else:
     ...:           res.append(ord(i)-65+26)
     ...:   return ''.join('%02d'%i for i in res)

In [187]: str2num('MyHome')
Out[187]: '382433141204'

use somestring.islower() and somestring.isUpper() 使用somestring.islower()和somestring.isUpper()

for lowercase its str(ord(somestring[i])-97) to get 0 - 25 for az 对于小写字母的str(ord(somestring [i])-97)对于az可以获得0-25

for uppercase str(ord(somestring[i])-65+26) to get 26 - 51 for AZ 对于大写str(ord(somestring [i])-65 + 26)获得AZ的26-51

i don't know how you want to code it but this should help alot. 我不知道你想如何编码,但这应该会有所帮助。 use ur Ascii table ;) 使用您的Ascii表;)

also this doesn't check for 2 digits so you're gonna need to figure that out too if you want things like 00, 01, etc... 这也不会检查2位数字,因此如果您想要00、01等内容,也需要弄清楚这一点。

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

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