简体   繁体   中英

Having problems with python permutations

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 in a text file, so eg it looks like (1,2,3,4,5,6) I want to open said .txt file and calculate all possible combinations of N of those 6 numbers up to N=4.

when i use itertools permutations

import itertools
x = [1, 2, 3, 4, 5, 6]
[p for p in itertools.product(x, repeat=2)]
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 
2), (2, 3), 
(2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 
5), (3, 6), 
(4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 
2), (5, 3), 
 (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6,5), 
(6, 6)]

it outputs the numbers like this, which i don't really want, since i can only get all combinations of one number of numbers at a time - But I want all possible combinations of N numbers with N ranging from 1 to 4, including repeats such as:

(1,1), (1,1,1) (1,1,1,1), (1,1,1,1) 

So I want it to have repeats, have combinations with a different number of members, however not go past 4 combinations of the number. I am really struggling with this concept! If anything doesn't make sense don't hesitate to ask me :)

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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM