问题描述
我需要创建一个python生成器,该生成器可以生成2D位矩阵的每种可能组合.每个尺寸的长度是可变的.
I need to create a python generator which yields every possible combination of a 2D bit matrix.The length of each dimention is variable.
因此对于2x2矩阵:
1.
00
00
1.
00
00
2.
10
00
2.
10
00
3.
11
00
3.
11
00
....
x.
00
01
x.
00
01
较大尺寸的尺寸(最大200 * 1000)也需要工作.最后,我将不需要所有组合.在每一行的总和为1的情况下,我只需要一次.但是在这种情况下,我需要所有组合.我会在屈服之前过滤它们.不需要打印.
Higher lengths of the dimentions (up to 200*1000) need to work too.In the end I will not need all of the combinations. I need only the once where the sum in each line is 1. But I need all combinations where this is the case. I would filter them before yielding. Printing is not required.
我想用它作为过滤器掩码来测试数据集的所有可能变化.产生这样的变体一定是一个普遍的问题.也许甚至还有一个很好的python库?
I want to use this as filter masks to test all possible variations of a data set.Generating variations like this must be a common problem. Maybe there is even a good library for python?
罗伯特
推荐答案
您可以对3x3二进制矩阵执行以下操作:
you could do something like this for 3x3 binary matrix:
for i in range(pow(2,9)):
p = '{0:09b}'.format(i)
print(p)
x = []
x.append([p[0],p[1],p[2]])
x.append([p[3],p[4],p[5]])
x.append([p[6],p[7],p[8]])
for i in range(3):
x[i] = map(int, x[i])
这篇关于具有各种可能组合的2D位矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!