Generating all combinations of a list in python

后端 未结 7 2156
不思量自难忘°
不思量自难忘° 2020-11-27 19:29

Here\'s the question:

Given a list of items in Python, how would I go by to get all the possible combinations of the items?

There are several similar questio

7条回答
  •  清歌不尽
    2020-11-27 20:04

    just thought i'd put this out there since i couldn't fine EVERY possible outcome and keeping in mind i only have the rawest most basic of knowledge when it comes to python and there's probably a much more elegant solution...(also excuse the poor variable names

    testing = [1, 2, 3]

    testing2= [0]

    n = -1

    def testingSomethingElse(number):

    try:
    
        testing2[0:len(testing2)] == testing[0]
    
        n = -1
    
        testing2[number] += 1
    
    except IndexError:
    
        testing2.append(testing[0])
    

    while True:

    n += 1
    
    testing2[0] = testing[n]
    
    print(testing2)
    
    if testing2[0] == testing[-1]:
    
        try:
    
            n = -1
    
            testing2[1] += 1
    
        except IndexError:
    
            testing2.append(testing[0])
    
        for i in range(len(testing2)):
    
            if testing2[i] == 4:
    
                testingSomethingElse(i+1)
    
                testing2[i] = testing[0]
    

    i got away with == 4 because i'm working with integers but you may have to modify that accordingly...

提交回复
热议问题