[英]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! 对于python我是一个非常大的菜鸟,所以任何帮助将不胜感激!
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. 可以说我在一个文本文件中有一个列表,范围从1-6,所以例如,它看起来像(1,2,3,4,5,6)我想打开所说的.txt文件并计算N的所有可能组合这6个数字中的最多N = 4。
when i use itertools permutations 当我使用itertools排列时
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: 它输出这样的数字,我不是真正想要的,因为我一次只能得到一个数字的所有组合-但是我希望所有N个数字的所有可能组合,N的范围是1-4,包括重复如:
(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. 因此,我希望它具有重复性,具有具有不同数量成员的组合,但是不要超过该数字的4种组合。 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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.