Python 根据另一个字符串的长度操作要重复的字符串

Python 根据另一个字符串的长度操作要重复的字符串,python,python-3.x,Python,Python 3.x,我正在从事一个python项目,其中我需要包含一个输入和另一个值(将被操纵) 比如说,, 如果我输入字符串'StackOverflow',并输入要操作的值'test',程序将通过重复和修剪字符串,使可操作变量等于字符数。这意味着'StackOverflow'和'test'将输出'testt' 这是我目前掌握的代码: originalinput = input("Please enter an input: ") manipulateinput = input("Please enter an i

我正在从事一个python项目,其中我需要包含一个输入和另一个值(将被操纵)

比如说,, 如果我输入字符串
'StackOverflow'
,并输入要操作的值
'test'
,程序将通过重复和修剪字符串,使可操作变量等于字符数。这意味着
'StackOverflow'
'test'
将输出
'testt'

这是我目前掌握的代码:

originalinput = input("Please enter an input: ")
manipulateinput = input("Please enter an input to be manipulated: ")
while len(manipulateinput) < len(originalinput):
originalinput=input(“请输入一个输入:”)
操纵输入=输入(“请输入要操纵的输入:”)
而len(操纵输入)

我正在考虑为
循环添加一个
,以继续其余的循环,但不确定如何使用它来有效地操作字符串。任何帮助都将不胜感激,谢谢。

尝试以下方式:

def trim_to_fit(to_trim, to_fit):
     # calculate how many times the string needs
     # to be self - concatenated
     times_to_concatenate = len(to_fit) // len(to_trim) + 1
     # slice the string to fit the target
     return (to_trim * times_to_concatenate)[:len(to_fit)]
它使用的是一个
X
与python中的一个字符串相乘的事实,它将字符串
X
连接起来

输出:

>>> trim_to_fit('test', 'stackoverflow')
'testtesttestt'
您还可以在字符串上创建一个无限循环:

# improved by Rick Teachey
def circular_gen(txt):
    while True:
        for c in txt:
            yield c
使用它:

>>> gen = circular_gen('test')
>>> gen_it = [next(gen) for _ in range(len('stackoverflow'))]
>>> ''.join(gen_it)
'testtesttestt'

试着这样做:

def trim_to_fit(to_trim, to_fit):
     # calculate how many times the string needs
     # to be self - concatenated
     times_to_concatenate = len(to_fit) // len(to_trim) + 1
     # slice the string to fit the target
     return (to_trim * times_to_concatenate)[:len(to_fit)]
它使用的是一个
X
与python中的一个字符串相乘的事实,它将字符串
X
连接起来

输出:

>>> trim_to_fit('test', 'stackoverflow')
'testtesttestt'
您还可以在字符串上创建一个无限循环:

# improved by Rick Teachey
def circular_gen(txt):
    while True:
        for c in txt:
            yield c
使用它:

>>> gen = circular_gen('test')
>>> gen_it = [next(gen) for _ in range(len('stackoverflow'))]
>>> ''.join(gen_it)
'testtesttestt'

一种
itertools.cycle
方法:

from itertools import cycle

s1 = 'Test'
s2 = 'StackOverflow'
result = ''.join(a for a, b in zip(cycle(s1), s2))
如果您提到纯文本-
a
是您的键,
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

一种
itertools.cycle
方法:

from itertools import cycle

s1 = 'Test'
s2 = 'StackOverflow'
result = ''.join(a for a, b in zip(cycle(s1), s2))
如果您提到纯文本-
a
是您的键,
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

您需要的是一种方法,可以一次又一次地将每个字符从
操作输入
字符串中取出,这样就不会用完字符

可以通过将字符串相乘来执行此操作,以便根据需要重复多次:

mystring = 'string'
assert 2 * mystring == 'stringstring'
但是要重复多少次呢?好的,您可以使用
len
获得字符串的长度:

assert len(mystring) == 6
因此,要确保您的字符串至少与另一个字符串一样长,可以执行以下操作:

import math.ceil # the ceiling function
timestorepeat  = ceil(len(originalinput)/len(manipulateinput))
newmanipulateinput = timestorepeat * manipulateinput
另一种方法是使用int除法,或
/

timestorepeat  = len(originalinput)//len(manipulateinput) + 1
newmanipulateinput = timestorepeat * manipulateinput
现在,您可以使用for循环而不会耗尽字符:

result = '' # start your result with an empty string 
for character in newmanipulateinput: 
    # test to see if you've reached the target length yet
    if len(result) == len(originalinput):
        break
    # update your result with the next character
    result += character 
    # note you can concatenate strings in python with a + operator 
print(result)

您需要的是一种方法,可以一次又一次地将每个字符从
操作输入
字符串中取出,这样就不会用完字符

可以通过将字符串相乘来执行此操作,以便根据需要重复多次:

mystring = 'string'
assert 2 * mystring == 'stringstring'
但是要重复多少次呢?好的,您可以使用
len
获得字符串的长度:

assert len(mystring) == 6
因此,要确保您的字符串至少与另一个字符串一样长,可以执行以下操作:

import math.ceil # the ceiling function
timestorepeat  = ceil(len(originalinput)/len(manipulateinput))
newmanipulateinput = timestorepeat * manipulateinput
另一种方法是使用int除法,或
/

timestorepeat  = len(originalinput)//len(manipulateinput) + 1
newmanipulateinput = timestorepeat * manipulateinput
现在,您可以使用for循环而不会耗尽字符:

result = '' # start your result with an empty string 
for character in newmanipulateinput: 
    # test to see if you've reached the target length yet
    if len(result) == len(originalinput):
        break
    # update your result with the next character
    result += character 
    # note you can concatenate strings in python with a + operator 
print(result)

这里有一些很好的Pythonic解决方案。。。但是,如果您的目标是理解
while
循环,而不是
itertools
模块,那么它们不会有帮助。在这种情况下,也许你只需要考虑如何用<代码> +/<代码>运算符来生长字符串,并用一个切片修剪:

originalinput = input("Please enter an input: ")
manipulateinput = input("Please enter an input to be manipulated: ")
output = ''
while len(output) < len(originalinput):
    output += manipulateinput
output = output[:len(originalinput)]
originalinput=input(“请输入一个输入:”)
操纵输入=输入(“请输入要操纵的输入:”)
输出=“”
而len(输出)

(请注意,在真正的Python代码中,这种字符串操作通常是不受欢迎的,您可能应该使用其他操作之一(例如,Reut Sharabani的答案)但是,如果你的目标是理解<代码>而<代码> >循环,而不是<代码>迭代器模块,它们将无济于事。在这种情况下,也许你只需要考虑如何用<代码> +/<代码>运算符来生长字符串,并用一个切片修剪:

originalinput = input("Please enter an input: ")
manipulateinput = input("Please enter an input to be manipulated: ")
output = ''
while len(output) < len(originalinput):
    output += manipulateinput
output = output[:len(originalinput)]
originalinput=input(“请输入一个输入:”)
操纵输入=输入(“请输入要操纵的输入:”)
输出=“”
而len(输出)

(请注意,在真正的Python代码中,这种字符串操作通常是不受欢迎的,您可能应该使用其他操作之一(例如,Reut Sharabani的答案).

问题是什么?…这是家庭作业问题吗?如果是,也没关系,但帮助建议你在问题的前面这样说。它让人们更有效地帮助你。另外:尝试至少包含一些你尝试破解过的代码。这是给我的问题:“关键字的重复次数足以匹配明文消息的长度。”。我可以将程序的其余部分添加到我已有的代码部分中,这只是我完成了一些课堂作业:)问题是什么?…这是一个家庭作业问题吗?如果是的话,这没关系,但是帮助建议你在问题的前面这样说。它让人们更有效地帮助你。另外:尝试至少包含一些你尝试尝试破解的代码。这是我收到的问题:关键字重复了足够多的次数,以匹配明文消息的长度。”。我可以将程序的其余部分添加到我已有的代码部分,这只是我完成了一些课堂作业:)OP使用的是Python 3-x,因此需要
/
。感谢您的帮助,非常感谢:)向上投票”无止境的