Having problems with python permutations

后端 未结 1 363
孤城傲影
孤城傲影 2021-01-28 13:03

I am having some problem with permutations! I am a really big noob when it comes to python so any help would be appreciated!

Lets say I have a list that ranges from 1-6

相关标签:
1条回答
  • 2021-01-28 13:37
    from itertools import product
    
    LIMIT = 4
    l1 = [1,2,3]
    
    results = []
    
    for i in range(1, LIMIT+1):
        results.extend(product(l1, repeat=i))
    
    print(results)
    

    will yield:

    [(1,), (2,), (3,), 
    (1, 1), (1, 2), (1, 3), 
    (2, 1), (2, 2), (2, 3), 
    (3, 1), (3, 2), (3, 3), 
    (1, 1, 1), (1, 1, 2), (1, 1, 3), 
    (1, 2, 1), (1, 2, 2), (1, 2, 3), 
    (1, 3, 1), (1, 3, 2), (1, 3, 3), 
    (2, 1, 1), (2, 1, 2), (2, 1, 3), 
    (2, 2, 1), (2, 2, 2), (2, 2, 3), 
    (2, 3, 1), (2, 3, 2), (2, 3, 3), 
    (3, 1, 1), (3, 1, 2), (3, 1, 3), 
    (3, 2, 1), (3, 2, 2), (3, 2, 3), 
    (3, 3, 1), (3, 3, 2), (3, 3, 3), 
    (1, 1, 1, 1), (1, 1, 1, 2), (1, 1, 1, 3), 
    (1, 1, 2, 1), (1, 1, 2, 2), (1, 1, 2, 3), 
    (1, 1, 3, 1), (1, 1, 3, 2), (1, 1, 3, 3), 
    (1, 2, 1, 1), (1, 2, 1, 2), (1, 2, 1, 3), 
    (1, 2, 2, 1), (1, 2, 2, 2), (1, 2, 2, 3), 
    (1, 2, 3, 1), (1, 2, 3, 2), (1, 2, 3, 3), 
    (1, 3, 1, 1), (1, 3, 1, 2), (1, 3, 1, 3), 
    (1, 3, 2, 1), (1, 3, 2, 2), (1, 3, 2, 3), 
    (1, 3, 3, 1), (1, 3, 3, 2), (1, 3, 3, 3), 
    (2, 1, 1, 1), (2, 1, 1, 2), (2, 1, 1, 3), 
    (2, 1, 2, 1), (2, 1, 2, 2), (2, 1, 2, 3), 
    (2, 1, 3, 1), (2, 1, 3, 2), (2, 1, 3, 3), 
    (2, 2, 1, 1), (2, 2, 1, 2), (2, 2, 1, 3), 
    (2, 2, 2, 1), (2, 2, 2, 2), (2, 2, 2, 3), 
    (2, 2, 3, 1), (2, 2, 3, 2), (2, 2, 3, 3),
    (2, 3, 1, 1), (2, 3, 1, 2), (2, 3, 1, 3), 
    (2, 3, 2, 1), (2, 3, 2, 2), (2, 3, 2, 3), 
    (2, 3, 3, 1), (2, 3, 3, 2), (2, 3, 3, 3), 
    (3, 1, 1, 1), (3, 1, 1, 2), (3, 1, 1, 3), 
    (3, 1, 2, 1), (3, 1, 2, 2), (3, 1, 2, 3), 
    (3, 1, 3, 1), (3, 1, 3, 2), (3, 1, 3, 3), 
    (3, 2, 1, 1), (3, 2, 1, 2), (3, 2, 1, 3), 
    (3, 2, 2, 1), (3, 2, 2, 2), (3, 2, 2, 3), 
    (3, 2, 3, 1), (3, 2, 3, 2), (3, 2, 3, 3), 
    (3, 3, 1, 1), (3, 3, 1, 2), (3, 3, 1, 3), 
    (3, 3, 2, 1), (3, 3, 2, 2), (3, 3, 2, 3), 
    (3, 3, 3, 1), (3, 3, 3, 2), (3, 3, 3, 3)]
    
    0 讨论(0)
提交回复
热议问题