Skip to main content
 首页 » 编程设计

python之随机输入创建的列表

2024年11月24日9lexus

创建一个程序,将随机字符输入到先前创建的列表中,然后能够转身将它们拉出。

变量 originalText 是代表一个字符的数字列表,而 key 是让程序知道输入多少的数字列表。

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

此代码能够运行,但不一致。我的第一个问题是所有 originalText 信息都被扔到 modifiedtect 的末尾,这没有让它正确地“展开”(取出“东西”)。

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

谢谢!

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 

请您参考如下方法:

这是否符合您的要求?

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