Python中的文本转换功能

Amp*_*ere 5 python python-2.7

我正在编写代码,因此你可以在字母表中的两个位置移动文本:'ab cd'应该变为'cd ef'.我正在使用Python 2,这是我到目前为止所得到的:

def shifttext(shift):
    input=raw_input('Input text here: ')
    data = list(input)
    for i in data:
        data[i] = chr((ord(i) + shift) % 26)
        output = ''.join(data)
    return output
shifttext(3)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

File "level1.py", line 9, in <module>
    shifttext(3)
File "level1.py", line 5, in shifttext
    data[i] = chr((ord(i) + shift) % 26)
TypError: list indices must be integers, not str
Run Code Online (Sandbox Code Playgroud)

所以我必须以某种方式将字母改为数字?但我以为我已经这样做了?

Mar*_*ers 9

您循环遍历字符列表,i因此是一个字符.然后,您尝试data将该i字符存储为使用该字符作为索引.那不行.

使用enumerate()得到的索引值:

def shifttext(shift):
    input=raw_input('Input text here: ')
    data = list(input)
    for i, char in enumerate(data):
        data[i] = chr((ord(char) + shift) % 26)
    output = ''.join(data)
    return output
Run Code Online (Sandbox Code Playgroud)

您可以使用生成器表达式简化此操作:

def shifttext(shift):
    input=raw_input('Input text here: ')
    return ''.join(chr((ord(char) + shift) % 26) for char in input)
Run Code Online (Sandbox Code Playgroud)

但现在你会注意到你的意思% 26不行; ASCII码点在26之后开始:

>>> ord('a')
97
Run Code Online (Sandbox Code Playgroud)

您需要使用该ord('a')值来代替模数; 减去将您的值放在0-25范围内,然后再添加它:

    a = ord('a')
    return ''.join(chr((ord(char) - a + shift) % 26) + a) for char in input)
Run Code Online (Sandbox Code Playgroud)

但这只适用于小写字母; 这可能没问题,但你可以通过降低输入来强制它:

    a = ord('a')
    return ''.join(chr((ord(char) - a + shift) % 26 + a) for char in input.lower())
Run Code Online (Sandbox Code Playgroud)

如果我们然后要求输出函数以将其集中在做好一个工作上,那么这将成为:

def shifttext(text, shift):
    a = ord('a')
    return ''.join(chr((ord(char) - a + shift) % 26 + a) for char in text.lower())

print shifttext(raw_input('Input text here: '), 3)
Run Code Online (Sandbox Code Playgroud)

并在交互式提示中使用它我看到:

>>> print shifttext(raw_input('Input text here: '), 3)
Input text here: Cesarsalad!
fhvduvdodgr
Run Code Online (Sandbox Code Playgroud)

当然,现在采用标点符号.最后修订版,现在只改变字母:

def shifttext(text, shift):
    a = ord('a')
    return ''.join(
        chr((ord(char) - a + shift) % 26 + a) if 'a' <= char <= 'z' else char
        for char in text.lower())
Run Code Online (Sandbox Code Playgroud)

我们得到:

>>> print shifttext(raw_input('Input text here: '), 3)
Input text here: Ceasarsalad!
fhdvduvdodg!
Run Code Online (Sandbox Code Playgroud)


Ash*_*ary 7

看起来你正在做cesar-cipher加密,所以你可以尝试这样的事情:

strs = 'abcdefghijklmnopqrstuvwxyz'      #use a string like this, instead of ord() 
def shifttext(shift):
    inp = raw_input('Input text here: ')
    data = []
    for i in inp:                     #iterate over the text not some list
        if i.strip() and i in strs:                 # if the char is not a space ""  
            data.append(strs[(strs.index(i) + shift) % 26])    
        else:
            data.append(i)           #if space the simply append it to data
    output = ''.join(data)
    return output
Run Code Online (Sandbox Code Playgroud)

输出:

In [2]: shifttext(3)
Input text here: how are you?
Out[2]: 'krz duh brx?'

In [3]: shifttext(3)
Input text here: Fine.
Out[3]: 'Flqh.'
Run Code Online (Sandbox Code Playgroud)

strs[(strs.index(i) + shift) % 26]:线以上手段找到字符的索引istrs,然后将移值添加到it.Now,对最终值(索引+偏移)申请%26向得到移位索引.传递时该移位的索引strs[new_index] 产生所需的移位字符.