Python多次打印列表

时间:2019-03-19 17:18:39

标签: python printing

我怎样才能使python只打印非法状态列表的一个副本? 当我只需要一份时,似乎列出它的时间太长了。

import itertools
import copy

BOAT = "B"


def illegalStates(roles):



    allstates = []
    legalstates = []
    illegalstates = []
    husbands = []
    wifes = []
    sisalto = []
    persons = copy.deepcopy(roles)
    persons.remove(BOAT)
    for i in range(1, len(persons)+1):
        els = [list(x) for x in itertools.combinations(persons, i)]
        allstates.extend(els)
    allstates.append([])

    for state in allstates:
        husbands.append([])
        wifes.append([])
        for henkilo in state:
            if henkilo == "H1":
                husbands[len(sisalto)-1].append(1)
            elif henkilo == "H2":
                husbands[len(sisalto)-1].append(2)
            elif henkilo == "H3":
                husbands[len(sisalto)-1].append(3)
            elif henkilo == "W1":
                wifes[len(sisalto)-1].append(1)
            elif henkilo == "W2":
                wifes[len(sisalto)-1].append(2)
            elif henkilo == "W3":
                wifes[len(sisalto)-1].append(3)
    for i in range(0, len(husbands)):
        if len(husbands[i]) == 0 or len(wifes[i]) == 0:
            legalstates.append(allstates[i])
    print(*legalstates)

    #--------------------------------
    return illegalstates


    if __name__ == "__main__":
        main()

这里是整个代码,以防万一它的其他部分使这种情况继续发生。 同样,在打印该列表时,它不会打印空白插槽。可以以某种方式打印吗?

1 个答案:

答案 0 :(得分:0)

while len(openlist) > 0:开始的循环包含对legalstates()的调用,其中包含对illegalstates()的调用,其中包含打印语句。因此,每次while循环...循环

时,您都在调用包含打印内容的方法。