使用python将字符移到左侧

使用python将字符移到左侧,python,python-2.7,Python,Python 2.7,我是python新手。 我想按位置移动char。例如 pos=3然后用z替换c,用y替换b,用x替换a,与用z替换c,用y替换b,用x替换a相同 我试过什么? 我试过使用硬编码值 for i in s: if ord(i) == 99: return z if ord(i) == 98: return x ... ... 是否有任何内置功能可用于执行此操作?或者其他更简单的方法来实现这一点 编辑: 如果字符串是“a

我是python新手。 我想按位置移动char。例如

pos=3然后用z替换c,用y替换b,用x替换a,与用z替换c,用y替换b,用x替换a相同

我试过什么? 我试过使用硬编码值

for i in s:
     if ord(i) == 99:
          return z
     if ord(i) == 98:
          return x
     ...
     ...
是否有任何内置功能可用于执行此操作?或者其他更简单的方法来实现这一点

编辑:

如果字符串是
“abcdef”
输出将是
“xyzabc”


用户输入
“AbCdEf”
然后输出将是
“XyZaBc”
您可以使用
ord
chr
组合使用:

ord("e") + 3
   -> 104
chr(ord("e") + 3)
   -> 'h'

您可以将
ord
chr
结合使用:

ord("e") + 3
   -> 104
chr(ord("e") + 3)
   -> 'h'

您可以将
ord
chr
结合使用:

ord("e") + 3
   -> 104
chr(ord("e") + 3)
   -> 'h'

您可以将
ord
chr
结合使用:

ord("e") + 3
   -> 104
chr(ord("e") + 3)
   -> 'h'
对于那些对更好的性能感兴趣的人,更新:

import string

def get_converter(pos):
        def rot(s, pos):
                offset = -pos % len(s)
                return s[offset:] + s[:offset]
        def getzip(charset, pos):
                return zip(charset, rot(charset, pos))
        def getdict(pos):
                return dict(getzip(string.ascii_lowercase, pos) + getzip(string.ascii_uppercase, pos))
        chardict = getdict(pos)
        def converter(c):
                return chardict.get(c, c)
        return converter

convert = get_converter(3)

assert convert("c") == "z"
assert convert("b") == "y"
assert convert("a") == "x"
assert convert("C") == "Z"
assert convert("B") == "Y"
assert convert("A") == "X"
assert convert("!") == "!"
对于那些对更好的性能感兴趣的人,更新:

import string

def get_converter(pos):
        def rot(s, pos):
                offset = -pos % len(s)
                return s[offset:] + s[:offset]
        def getzip(charset, pos):
                return zip(charset, rot(charset, pos))
        def getdict(pos):
                return dict(getzip(string.ascii_lowercase, pos) + getzip(string.ascii_uppercase, pos))
        chardict = getdict(pos)
        def converter(c):
                return chardict.get(c, c)
        return converter

convert = get_converter(3)

assert convert("c") == "z"
assert convert("b") == "y"
assert convert("a") == "x"
assert convert("C") == "Z"
assert convert("B") == "Y"
assert convert("A") == "X"
assert convert("!") == "!"
对于那些对更好的性能感兴趣的人,更新:

import string

def get_converter(pos):
        def rot(s, pos):
                offset = -pos % len(s)
                return s[offset:] + s[:offset]
        def getzip(charset, pos):
                return zip(charset, rot(charset, pos))
        def getdict(pos):
                return dict(getzip(string.ascii_lowercase, pos) + getzip(string.ascii_uppercase, pos))
        chardict = getdict(pos)
        def converter(c):
                return chardict.get(c, c)
        return converter

convert = get_converter(3)

assert convert("c") == "z"
assert convert("b") == "y"
assert convert("a") == "x"
assert convert("C") == "Z"
assert convert("B") == "Y"
assert convert("A") == "X"
assert convert("!") == "!"
对于那些对更好的性能感兴趣的人,更新:

import string

def get_converter(pos):
        def rot(s, pos):
                offset = -pos % len(s)
                return s[offset:] + s[:offset]
        def getzip(charset, pos):
                return zip(charset, rot(charset, pos))
        def getdict(pos):
                return dict(getzip(string.ascii_lowercase, pos) + getzip(string.ascii_uppercase, pos))
        chardict = getdict(pos)
        def converter(c):
                return chardict.get(c, c)
        return converter

convert = get_converter(3)

assert convert("c") == "z"
assert convert("b") == "y"
assert convert("a") == "x"
assert convert("C") == "Z"
assert convert("B") == "Y"
assert convert("A") == "X"
assert convert("!") == "!"

如果你使用Python 2,并且你想转换整个句子,考虑制作一个字符转换表。 请记住,作为改进Python Unicode字符串处理的一部分,string.maketrans和string.translate在Python3中已被删除

使用负值或正值向左/向右移动。

>P>如果你使用Python 2,并且你想转换整个句子,考虑制作一个字符转换表。 请记住,作为改进Python Unicode字符串处理的一部分,string.maketrans和string.translate在Python3中已被删除

使用负值或正值向左/向右移动。

>P>如果你使用Python 2,并且你想转换整个句子,考虑制作一个字符转换表。 请记住,作为改进Python Unicode字符串处理的一部分,string.maketrans和string.translate在Python3中已被删除

使用负值或正值向左/向右移动。

>P>如果你使用Python 2,并且你想转换整个句子,考虑制作一个字符转换表。 请记住,作为改进Python Unicode字符串处理的一部分,string.maketrans和string.translate在Python3中已被删除

使用负值或正值向左/向右移动。

根据您的要求,我知道您需要演示一个。实现起来非常简单。我将为您提供函数大纲伪代码:

char substitutionCipher(int leftRotateBy, char alphabet) {
    int pos = (int) alphabet - (int) 'a';
    pos -= leftRotateBy;
    if(pos < 0) {
       pos += 26;
    }
    return (char) (pos + (int) 'a');
}
char substitutionCipher(int-leftRotateBy,char-alphabet){
int pos=(int)字母表-(int)'a';
pos-=左旋转比;
如果(位置<0){
pos+=26;
}
返回字符(pos+(int)“a”);
}

请注意,此大纲仅适用于小写字母。您可以对其进行修改,使其也适用于大写字母。谢谢

根据您的要求,我知道您需要演示一个。实现起来非常简单。我将为您提供函数大纲伪代码:

char substitutionCipher(int leftRotateBy, char alphabet) {
    int pos = (int) alphabet - (int) 'a';
    pos -= leftRotateBy;
    if(pos < 0) {
       pos += 26;
    }
    return (char) (pos + (int) 'a');
}
char substitutionCipher(int-leftRotateBy,char-alphabet){
int pos=(int)字母表-(int)'a';
pos-=左旋转比;
如果(位置<0){
pos+=26;
}
返回字符(pos+(int)“a”);
}

请注意,此大纲仅适用于小写字母。您可以对其进行修改,使其也适用于大写字母。谢谢

根据您的要求,我知道您需要演示一个。实现起来非常简单。我将为您提供函数大纲伪代码:

char substitutionCipher(int leftRotateBy, char alphabet) {
    int pos = (int) alphabet - (int) 'a';
    pos -= leftRotateBy;
    if(pos < 0) {
       pos += 26;
    }
    return (char) (pos + (int) 'a');
}
char substitutionCipher(int-leftRotateBy,char-alphabet){
int pos=(int)字母表-(int)'a';
pos-=左旋转比;
如果(位置<0){
pos+=26;
}
返回字符(pos+(int)“a”);
}

请注意,此大纲仅适用于小写字母。您可以对其进行修改,使其也适用于大写字母。谢谢

根据您的要求,我知道您需要演示一个。实现起来非常简单。我将为您提供函数大纲伪代码:

char substitutionCipher(int leftRotateBy, char alphabet) {
    int pos = (int) alphabet - (int) 'a';
    pos -= leftRotateBy;
    if(pos < 0) {
       pos += 26;
    }
    return (char) (pos + (int) 'a');
}
char substitutionCipher(int-leftRotateBy,char-alphabet){
int pos=(int)字母表-(int)'a';
pos-=左旋转比;
如果(位置<0){
pos+=26;
}
返回字符(pos+(int)“a”);
}


请注意,此大纲仅适用于小写字母。您可以对其进行修改,使其也适用于大写字母。谢谢

我不明白这个要求。你能提供一个输入样本和你想得到的结果吗?@Mureinik查看更新。我不理解这个要求。你能提供一个输入样本和你想得到的结果吗?@Mureinik查看更新。我不理解这个要求。你能提供一个输入样本和你想得到的结果吗?@Mureinik查看更新。我不理解这个要求。你能提供一个输入样本和你想得到的结果吗?@Mureinik看到更新了。这是一个原则,我想他可以编写算法来处理极端情况…这是一个原则,这是一个原则,我猜他可以写算法来处理极端情况…这是原则,我猜他可以写算法来处理极端情况…这是原则,这是原则,我想他可以编写算法来处理极端情况……Find函数是线性运行的time@IceArdor问题不是关于性能优化,是吗?;-)查找函数具有线性运行time@IceArdor问题不是关于性能优化,是吗?;-)查找函数具有线性运行time@IceArdor问题不是关于性能优化,是吗?;-)查找函数具有线性运行time@IceArdor这个问题谈不上