Python重新循环一个Lis

2024-09-28 17:22:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张单子

masterList = [42,28,14,28,14,28,42,14]

此列表用于根据上述顺序生成第二个较长的列表。我希望通过该列表迭代并自动通过该列表重申,直到满足条件为止。你知道吗

对于回复,抱歉这里有一个示例:-

templateList = [42,28,14,28,14,28,42,14]
newList =[]

while newList < 1000:
    for i in templateList:
        a = newList[-1] + i
    newList.append(a)

print(newList)

我想要的结果是: 新列表=[84、140、168、224、252、308、392、420、504、560、588、644、672、728、812、840、924、980]


Tags: in示例列表for顺序单子printappend
2条回答

也许这就是你想要的:

condition = True # CHANGE ME
lst = [1,2,3]
i = 0
while (condition):
    elem = lst[i]
    print(elem) # DO STUFF
    i = (i+1) % len(lst)

假设condition是您想要的条件,一种可能性是:

while condition:
    for i in range(len(masterList)):
        pass # Do the stuff you want

内部循环是for循环,它遍历列表。外部循环确保在for循环结束后,另一个for循环开始(前提是条件保持为true)。你知道吗

相关问题 更多 >