随机输入到创建的列表中

时间:2019-03-14 19:12:42

标签: python

创建一个程序,将随机字符输入到先前创建的列表中,然后可以将其转过来并拉出。

变量originalText是表示字符的数字列表,而key是程序知道要输入多少的数字列表。

我的原始计划是获取原始文本,将第一个值输入到新列表中,然后添加x数量的“ stuff”,直到结束。一旦输入了“东西”,它将在再次循环之前输入另一个原始文本字符,直到原始文本超出要添加到ModifyedText的值为止。

此代码可以运行,但不一致。我的第一个问题是,所有originalText信息都被扔到了Modifytect的末尾,这并没有使其正确“展开”(取出“填充”)。

我认为这是一个数学问题。有什么建议吗?

谢谢!

def fixStuff(originalText, key, direction):
#0 adds Stuff, 1 removes Stuff
generate = key[4] #= getMod(key,4) #this variable is used for deciding how much Stuff to input.

originalSize = len(originalText)
print(originalSize)
random.randint(32,126) #used to generate random characters. 

i = 1 #used for counting
origSpot = 0 #used to track position in original list

modifiedText = []
print(generate)

if direction is 0:
    #originalSize = originalSize * generate # the total size of the array after Stuff.
    print("inserting Stuff")

    while origSpot < originalSize: #whatever this is
        #after every originalSize digit, input generate(th) digits

        print(origSpot)
        modifiedText.insert((i),originalText[origSpot])
        x = 0 #used for inserting. It must be reset before this while runs again. 
        while x <= generate:
            modifiedText.insert((i),random.randint(32,126))
            x = x + 1

            #print(x)
       #print(i)
        i = i + generate #increment the gradiant so we dump the next load of Stuff after the next real digit
        origSpot = origSpot + 1

i = 0
if direction is not 0:
    print("remove el Stuffo")

        #take every generate(th) digit and put it in a new array.
    while origSpot < originalSize: #whatever this is
        #after every originalSize digit, input generate(th) digits

        print(origSpot)
        modifiedText.insert((i),originalText[origSpot])
        x = 0 #used for inserting. It must be reset before this while runs again. 
        while x <= generate:
            origSpot = origSpot + 1
            x = x + 1

            #print(x)
       #print(i)
        i = i + 1 #increment the gradiant so we dump the next load of Stuff after the next real digit
        print("ree!")

return modifiedText

1 个答案:

答案 0 :(得分:1)

这是您想要的吗?

import random


def addStuff(originalText, n):
    """
    returns a list with n random characters inserted after each element in originalText
    """
    modifiedText = []
    for elem in originalText:
        modifiedText.append(elem)
        for _ in range(n):
            modifiedText.append(chr(random.randint(32, 126)))
    return modifiedText


def removeStuff(modifiedText, n):
    """return a list with every (n+1)th char in modifiedText.
    Undoes the action of addStuff
    """
    originalText = []
    for i in range(0, len(modifiedText), n + 1):
        originalText.append(modifiedText[i])
    return originalText


v = addStuff("hello", 2)
print(v)
# prints e.g. ['h', 'm', 'w', 'e', '^', '0', 'l', '>', 'Q', 'l', '/', '}', 'o', '6', 'L']
r = removeStuff(v, 2)
print(r)  # ['h', 'e', 'l', 'l', 'o']

我将工作分为两个功能,因为它们执行两个不同的,不重叠的任务。我使用append和for循环构建了列表。您可以使用while循环来完成此操作,但是我认为for循环不太容易出错。为了完整起见,这是while循环版本:

def addStuff(originalText, n):
    """
    returns a list with n random characters inserted after each element in originalText
    """
    modifiedText = []
    i = 0
    while i < len(originalText):
        modifiedText.append(originalText[i])
        x = 0
        while x < n:
            modifiedText.append(chr(random.randint(32, 126)))
            x = x + 1
        i = i + 1
    return modifiedText


def removeStuff(modifiedText, n):
    """return a list with every (n+1)th char in modifiedText.
    Undoes the action of addStuff
    """
    originalText = []
    i = 0
    while i < len(modifiedText):
        originalText.append(modifiedText[i])
        # skipping over n random elements
        i = i + 1 + n
    return originalText