Can anyone solve this?

Cezar Ionescu ionescu at pik-potsdam.de
Mon Jun 26 04:02:50 EDT 2000


[Rene]

[...snip]
> I need the code to give every possible combination of the lists in
> inputlist.
[...snip again]
> 
> Anyone able to solve this?

Uhhh, I *think* so:

def list_comb(inputlist):
    outputlist = [[]]
    for list in inputlist:
        newoutputlist = []
        for incomplete in outputlist:
            for element in list:
                newoutputlist.append(incomplete + [element])
        outputlist = newoutputlist
    return outputlist

if __name__ == "__main__":
    a = [1, 2, 3]
    b = ["c", 32]
    c = [1, 2]
    inputlist = [a, b, c]
    print list_comb(inputlist)

This prints the results you wanted. However, it probably won't do what
you want for inputlist with one element, or with no elements, so please
take this as "a first approximation" only.

Best,
Cezar.



More information about the Python-list mailing list