python排列有问题 [英] Having problems with python permutations

查看:49
本文介绍了python排列有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在排列方面遇到了一些问题!当涉及到python时,我是一个非常大的菜鸟,因此对您的任何帮助将不胜感激!

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

让我们说我在一个文本文件中的列表范围是1-6,例如 看起来像(1,2,3,4,5,6)我想打开所说的.txt文件,并计算这6个数字中N的所有可能组合,直到N = 4.

Lets say I have a list that ranges from 1-6 in a text file, so e.g 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.

当我使用itertools排列

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

它输出这样的数字,我不是真正想要的,因为我一次只能得到一个数字的所有组合-但是我希望所有N个数字的所有可能组合,N的范围是1到4 ,包括重复的内容,例如:

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) 

因此,我希望它具有重复性,具有具有不同数量成员的组合,但是不要超过该数字的4种组合.我真的在为这个概念而苦苦挣扎!如果没有任何意义,请随时问我:)

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)

将产生:

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

这篇关于python排列有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆