Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中数组中1和0的组合_Python_Python 2.7_Combinations_Itertools - Fatal编程技术网

Python中数组中1和0的组合

Python中数组中1和0的组合,python,python-2.7,combinations,itertools,Python,Python 2.7,Combinations,Itertools,我想在二维数组中组合1和0,如下所示: [[ 1, 1, 1, 1, 0, 0, 0, 0 ], [ 1, 1, 1, 0, 1, 0, 0, 0 ], [ 1, 1, 1, 0, 0, 1, 0, 0 ], [ 1, 1, 1, 0, 0, 0, 1, 0 ], [ 1, 1, 1, 0, 0, 0, 0, 1 ], . . . ] 这意味着四个1和四个0的组合。我查看了itertools模块的permutations()和combinations(),但找不到任何合适的函数来

我想在二维数组中组合1和0,如下所示:

[[ 1, 1, 1, 1, 0, 0, 0, 0 ],
 [ 1, 1, 1, 0, 1, 0, 0, 0 ],
 [ 1, 1, 1, 0, 0, 1, 0, 0 ],
 [ 1, 1, 1, 0, 0, 0, 1, 0 ],
 [ 1, 1, 1, 0, 0, 0, 0, 1 ],
 .
 .
 .
]
这意味着四个1和四个0的组合。我查看了
itertools
模块的
permutations()
combinations()
,但找不到任何合适的函数来执行此组合。

您正在生成一个

简单但朴素的方法是使用一个集合来过滤重复的组合:

>>> from itertools import permutations
>>> seen = set()
>>> for combo in permutations([1] * 4 + [0] * 4):
...     if combo not in seen:
...         seen.add(combo)
...         print combo
...
(1, 1, 1, 1, 0, 0, 0, 0)
(1, 1, 1, 0, 1, 0, 0, 0)
(1, 1, 1, 0, 0, 1, 0, 0)
(1, 1, 1, 0, 0, 0, 1, 0)
(1, 1, 1, 0, 0, 0, 0, 1)
(1, 1, 0, 1, 1, 0, 0, 0)
# ...
(0, 0, 1, 0, 1, 0, 1, 1)
(0, 0, 1, 0, 0, 1, 1, 1)
(0, 0, 0, 1, 1, 1, 1, 0)
(0, 0, 0, 1, 1, 1, 0, 1)
(0, 0, 0, 1, 1, 0, 1, 1)
(0, 0, 0, 1, 0, 1, 1, 1)
(0, 0, 0, 0, 1, 1, 1, 1)
或者,一次性生成整个序列:

set(permutations([1] * 4 + [0] * 4))
但这失去了排列产生的顺序

由于
permutations()
将4
1
和4
0
视为单个字符,并且一个
1
与另一个
交换被视为唯一的置换,因此需要该集合

您还可以使用序列中的顺序,以避免使用集合:

last = (1,) * 8
for combo in permutations([1] * 4 + [0] * 4):
    if combo < last:
        last = combo
        print combo
last=(1,)*8
对于排列中的组合([1]*4+[0]*4):
如果组合<最后一个:
最后一个=组合
打印组合

这种方法在2n中是幼稚的!在我们只需要(2n选择n)个元素的地方产生置换。对于您的情况,这是40320个排列,我们只需要生成70个。

您也可以使用
组合
直接生成唯一的组合:

n = 8
n1 = 4
for x in itertools.combinations( xrange(n), n1 ) :
    print [ 1 if i in x else 0 for i in xrange(n) ] 

[1, 1, 1, 1, 0, 0, 0, 0]
[1, 1, 1, 0, 1, 0, 0, 0]
[1, 1, 1, 0, 0, 1, 0, 0]
[1, 1, 1, 0, 0, 0, 1, 0]
...
[0, 0, 0, 1, 1, 1, 0, 1]
[0, 0, 0, 1, 1, 0, 1, 1]
[0, 0, 0, 1, 0, 1, 1, 1]
[0, 0, 0, 0, 1, 1, 1, 1]
这比排列更有效,因为您不会迭代不需要的解决方案


直觉是,你正在试图找到所有可能的方法,在长度为8的序列中匹配四个“1”;这就是我们的确切定义。这个数字是
C(8,4)=8!/(4!*4!)=70
。相反,使用
置换的解决方案迭代
8!=40320
候选解决方案。

或者只是
集合(itertools.permutations([1]*4+[0]*4))
?@tobias_k:是的,但是你不得不一次生成整个集合,顺序可能很重要。因为集合只有70个元素,我认为这不是什么问题,也不是像
排序(set(…),reverse=True)那样排序集合
+感谢托拜厄斯。已经完成了。但是,我怎样才能像我给出的问题那样,把它表述成一个矩阵呢。像[…],[…],[…]format@user3812253:
permutations()
生成元组;您可以很容易地对每个尚未看到的结果调用
list()
,并将其添加到list.related+1中。但是,我仍然更喜欢
置换
解决方案,尤其是单行程序,因为代码的作用很明显,而且生成开销也没有那么大(但可能需要更多位).这正是我想要的。非常感谢我。@tobias_k:使用
permutations
的解决方案可能更具可读性,但尝试将其与
n>15
一起使用……啊,找到了术语:多集置换。@usualme是的,这基本上就是我想在我的评论中表达的内容。;-)