2015-01-26 15 views
1

我正在开发一个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循环继续休息,但我不知道我怎么会用这个有效操作字符串。任何帮助将不胜感激,谢谢。

+0

问题再次出现了什么?... – 2015-01-26 18:18:21

+0

这是一个家庭作业问题吗?可以的,如果是的话,但是帮助建议你这样说在你的问题前面,它可以让人们更有效地帮助你,另外:试着至少包含一些你试过的代码,以便对它进行破解 – 2015-01-26 18:18:29

+0

这是我给出的问题:“keywor d重复 足够的时间以匹配明文消息的长度。“。我可以将其余的程序添加到我已经拥有的代码段中,这只是我完成了一些作业:) – KBHO 2015-01-26 18:22:24

回答

2

这里有一些很好的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)] 

(请注意,这种字符串操作一般是在实际的Python代码皱起了眉头,你应该使用其他人之一(例如Reut Sharabani的回答)

+0

似乎是这个任务的目标是理解比'while'循环更多的字符串操作。 – 2015-01-26 18:33:06

+0

我刚刚试过这个程序,它似乎完美的工作,因为它允许我操纵字符串,以匹配原始输入的字符长度。对不起,我对我的指示有些模糊,但是谢谢@xnx :) – KBHO 2015-01-26 18:37:29

+0

@xnx对不起,打扰了你,但你知道我怎么能改进这个程序,所以它会忽略输入中的空格吗? – KBHO 2015-01-26 19:08:34

2

尝试这样:

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)] 

它采用slicing,而事实上,一个X的乘法和蟒蛇会连接字符串X次的字符串。

输出:

>>> trim_to_fit('test', 'stackoverflow') 
'testtesttestt' 

您还可以创建无限循环generator在字符串:

# 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' 
+0

OP使用Python 3-x,所以需要''''。 – xnx 2015-01-26 18:21:04

+0

@xnx谢谢,修复 – 2015-01-26 18:22:08

+0

感谢您的帮助,非常感谢:) – KBHO 2015-01-26 18:40:43

3

itertools.cycle方法:

from itertools import cycle 

s1 = 'Test' 
s2 = 'StackOverflow' 
result = ''.join(a for a, b in zip(cycle(s1), s2)) 

给你提明文 - a是你的钥匙和b将在明文字符 - 所以你可以用它来得心应手也将manipuate配对...

我正在猜想你会喜欢的东西来结束:

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 
+0

感谢您的帮助,非常感谢:) – KBHO 2015-01-26 18:38:45

0

你需要的是一种方法来一遍又一遍地让每个字符您的manipulateinput串的,所以你不要跑出来的字符。

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) 
+0

感谢您的帮助,非常感谢:) – KBHO 2015-01-26 18:41:36

+1

没有问题。代码不起作用。现在修复。顺便说一句,对人们说“谢谢”的最好方法是提出答案,并/或将其标记为已接受。 – 2015-01-26 18:42:24

+0

由于我的声望不到15,我似乎无法赞成,但我会尝试所有这些(如果它允许我),我会将最相关的标记标记为已接受。再次感谢 – KBHO 2015-01-26 18:45:14