我正在开发一个 python 项目,我需要在其中包含一个输入和另一个值(将被操纵)。
例如, 如果我输入字符串 'StackOverflow' 和要操作的值 'test',程序将使可操作变量等于字符数,通过重复并修剪琴弦。这意味着 'StackOverflow' 和 'test' 将输出 'testtesttestt'。
这是我目前的代码:
originalinput = input("Please enter an input: ")
manipulateinput = input("Please enter an input to be manipulated: ")
while len(manipulateinput) < len(originalinput):
我正在考虑包括一个 for 循环来继续剩下的,但我不确定我将如何使用它来有效地操作字符串。任何帮助将不胜感激,谢谢。
请您参考如下方法:
itertools.cycle 方法:
from itertools import cycle
s1 = 'Test'
s2 = 'StackOverflow'
result = ''.join(a for a, b in zip(cycle(s1), s2))
鉴于您提到了 plaintext - a 是您的 key ,而 b 将是明文中的字符 - 因此您也可以使用它轻松操纵配对...
我猜你最终会得到类似这样的东西:
result = ''.join(chr(ord(a) ^ ord(b)) for a, b in zip(cycle(s1), s2))
# '\x07\x11\x12\x17?*\x05\x11&\x03\x1f\x1b#'
original = ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(cycle(s1), result))
# StackOverflow
