【问题标题】:Python: Moving letters within the alphabet in a stringPython:在字符串中的字母表中移动字母
【发布时间】:2016-01-01 08:40:56
【问题描述】:

我一直在使用 Python 并试图将字符串中的所有字母移动字母表中的 n 个空格,但是我遇到了很多错误。

print("Cryptography")
userInput = str(input("Write anything that you'd like to encrypt here: "))
userNumberInput = int(input("Please choose a number without decimals here: "))
userInput = userInput.lower()
wordCount = userInput.split()
loopRunner = int(0)
letterCount = list()
for word in range(len(wordCount)):
    letterCount.insert(loopRunner,len(wordCount[loopRunner]))
    print(letterCount[loopRunner])
    loopRunner = loopRunner + 1

outputString = "" .join((chr(97 + (ord(letter) -97 + userNumberInput) % 26) for letter in userInput))
loopRunner = 0
for word in range(len(wordCount)):
    outputString2 = [outputString[i:i+letterCount[loopRunner]] for i in range (0, len(outputString), letterCount[loopRunner])]
    loopRunner = loopRunner + 1
finalResult = " " .join(outputString2)
print(finalResult)

到目前为止,它完成了我需要它做的事情,但是当我尝试运行它时,它似乎也将空格计为字母,我不知道如何排除它们,同时将它们保留在最终结果中,就像它们一样是。

我想知道的另一件事是我是否有任何方法可以防止字母变成小写字母并保持代码正常运行?

我已经尝试了好几个小时,但一直没有找到好的解决方案。

【问题讨论】:

  • 您是要同时更改空格还是只更改字母并将空格保留为空格?
  • 我想更改字母并将它们所在的空格保留为空格。我将如何存储空间的位置?我了解整个过程,但我什至不知道从哪里开始编写代码。你能给我一个简单的例子吗?
  • 我正在研究一个解决方案,但它可能看起来像这样:在开始时:将所有空格的索引位置保存在列表中 userInput = userInput.replace(' ', '' ) 并在最后再次在这些索引位置添加空格。

标签: python string for-loop whitespace


【解决方案1】:

在不编辑已有内容的情况下执行此操作的一种方法是对所有存在空格的位置进行索引,并将这些索引位置保存为字符串。请注意我的例子很混乱,你应该尝试在没有 try/except 结构的情况下实现。

n = 0
spacesLocation = []
while(True):
    try:
        spacesLocation.append(userInput.index(' '))
        userInput = userInput.replace(' ', '', 1)
        n+=1
    except:
        n = 0
        break

然后在最后添加它们:

while(n < len(spacesLocation)):#goes through where all the spaces are
    finalResult = finalResult[:spacesLocation[n]+n] + ' ' +finalResult[spacesLocation[n]+n:]#adds spaces where they go
    n+=1#re-iterates to go through where all the spaces should be

所以你的整个事情看起来像这样:

print("Cryptography")
userInput = str(input("Write anything that you'd like to encrypt here: "))
n = 0
spacesLocation = []
while(True):
    try:
        spacesLocation.append(userInput.index(' '))
        userInput = userInput.replace(' ', '', 1)
        n+=1
    except:
        n = 0
        break
userNumberInput = int(input("Please choose a number without decimals here: "))
userInput = userInput.lower()
wordCount = userInput.split()
loopRunner = int(0)
letterCount = list()
for word in range(len(wordCount)):
    letterCount.insert(loopRunner,len(wordCount[loopRunner]))
    print(letterCount[loopRunner])
    loopRunner = loopRunner + 1

outputString = "" .join((chr(97 + (ord(letter) -97 + userNumberInput) % 26) for letter in userInput))
loopRunner = 0
for word in range(len(wordCount)):
    outputString2 = [outputString[i:i+letterCount[loopRunner]] for i in range (0, len(outputString), letterCount[loopRunner])]
    loopRunner = loopRunner + 1
finalResult = " " .join(outputString2)
while(n < len(spacesLocation)):
    finalResult = finalResult[:spacesLocation[n]+n] + ' ' +finalResult[spacesLocation[n]+n:]
    n+=1
print(finalResult)

如果您对它的工作原理有任何疑问,请发表评论,因为 stackoverflow 旨在增加理解,而不仅仅是提供解决方案。

【讨论】:

  • 非常感谢!这正是我一直在寻找的,我是否正确假设/理解该代码可以调整为还存储大写字母或任何字符的索引位置,以便我可以拥有例如 hello 的世界2 的 userNumberInput 变成 jgnNq yqtnf 而不是 jgnnq yqtnf?
  • 你可以修改它来做到这一点,但它会有点麻烦。
  • 我在玩弄了一下之后才意识到这一点。谢谢:)
【解决方案2】:

你可以使用字符串模块和移位函数如下:

import string

def shift():
    userInput = input("Write anything that you'd like to encrypt here: ")
    userNumberInput = int(input("Please choose a number without decimals here: "))
    letters=' '+string.ascii_letters
    if userNumberInput > len(letters): userNumberInput = userNumberInput%len(letters)
    d={k:v for k,v in zip(letters,' '+letters[userNumberInput:]+letters[:userNumberInput])}
    return ''.join([x.replace(x,d[x]) for x in userInput])

函数不改变空格位置并将userInput中的所有字母完全移动userNumberInput数字

【讨论】:

  • 非常感谢!它可以完成我需要它做的事情,并在涉及编码时让我对其他选项有相当多的了解。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 2012-01-11
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
  • 2016-01-05
  • 2014-07-17
相关资源
最近更新 更多