【问题标题】:How to shift letters in the alphabet using python? [duplicate]如何使用python移动字母表中的字母? [复制]
【发布时间】:2021-11-17 16:11:57
【问题描述】:

我想使用 python 移动字母表中的每个字母(例如 a→b,b→c,...)。当你写一个像“汽车”这样的词时,它会改变字母,新词是“Dbs”(C→D, a→b, r→s)。到目前为止,这是我的代码,但它不起作用并且效率不高:

def shift(letter):
      switch={
      "a":"b",
      "b":"c",
      "c":"d",
      "d":"e",
      "e":"f",
      "f":"g",
      "g":"h",
      "h":"i",
      "i":"j",
      "j":"k",
      "k":"l",
      "l":"m",
      "m":"n",
      "n":"o",
      "o":"p",
      "p":"q",
      "q":"r",
      "r":"s",
      "s":"t",
      "t":"u",
      "u":"v",
      "v":"w",
      "w":"x",
      "x":"y",
      "y":"z",
      "z":"a"
      }
      return switch.get(letter,"Invalid input")
      
shift("c")

【问题讨论】:

    标签: python python-3.x encoder letter alphabet


    【解决方案1】:

    你可以用这个:

    def shft_char(word,num):
        return ''.join(map(chr,([ord(c)+num for c in word])))
    

    输出:

    >>> shft_char('Car',1)
    'Dbs'
    

    【讨论】:

      猜你喜欢
      • 2016-01-01
      • 2017-10-10
      • 2021-12-20
      • 1970-01-01
      • 2020-06-11
      • 2017-01-08
      • 1970-01-01
      • 2016-02-01
      • 2020-03-17
      相关资源
      最近更新 更多