2015-01-26 16 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