在Python中创建列表列表时的无限循环

时间:2016-10-09 12:40:12

标签: python

我在创建包含所有可能的工作计划的列表列表时遇到了问题。一个简单的例子是:

工作日班次......第二天工作日,晚上或休息:[日,[白天,夜晚,休息]]

工作夜班,第二天休息: [晚上,关闭]

如果关闭,第二天你可以白天,晚上工作或者休息: [关闭,[白天,黑夜,关闭]

3天的列表清单如下:

[day,[[day,[day,night,off]],[night,[off]],[off,[day,night,off]]]

我能够为第二天创建一个时间表:

def schedule(i):
    Tree = [0]*3
    Tree[0] = i

    for x in range(len(Tree)-1):
        if Tree[x] == "day":
            Tree[x+1] = "[day, night, off]"
        elif Tree[x] == "night":
            Tree[x+1] = "off"
        else:
            Tree[x+1] = "[day, night, off]"


print(Tree)

schedule('day')

但是,当我尝试重复创建列表列表以将计划延长一周左右时,我会得到错误按摩和无限循环。这是一个例子:

def RecSched(i, count):
    RecSched = [0]*7
    RecSched[0] = i
    while count > 0:
        for x in range(len(RecSched)-1):
            if RecSched[x] == "day":
                RecSched[x+1] = "[day, night, off]"
            elif RecSched[x] == "night":
                RecSched[x+1] = "off"
            else:
                RecSched[x+1] = "[day, night, off]"

    count = count - 1
    RecSched(x+1, count)

RecSched('day', 7)

我是新人并且在学习。你能帮我弄清楚我做错了什么,最好的办法是什么?

0 个答案:

没有答案