问题描述
我一直令人头大我的脑子出3个小时了,但我还是不明白这一点,所以我问在这里。 (我在标题中写道Python的,但是这可能是pretty太多任何语言)
I have been wracking my brains out for 3 hours straight, but I still don't get it, so I am asking here. (I wrote Python in the title, but this could be for pretty much any language)
让我们假设我有一组位(但它也可以是在一个确定的范围内的整数)的固定长度为n的,让我们说5
Let's assume I have an array of bits (but it may also be integers in a defined range) of fixed length n, let's say 5.
array=[0,1,1,0,0]
现在,我怎么产生的所有阵列,这在数量范围是可能的(以比特的情况下,2)。
Now, how do I generate ALL arrays, which are possible in the number range (in the case of bits, 2).
所以:
[0,0,0,0,0], [0,0,0,0,1], [0,0,0,1,0], [0,0,0,1,1] ...
我试图寻找这里的解决方案,但我总能找到的东西是相似的,但并不完全解决我的问题。
I have tried searching for a solution here, but I always find something which is similar, but which doesn't quite solve my problem.
要解决这个问题,我已经试过各种循环,但我总是最后要么得到一个可能不止一次(不应发生),或者没有得到所有可能的。
To solve this, I have tried various loops, but I always end up either getting one possibility more than once (should not happen), or not getting all possible ones.
我可以管理与if语句(检查组合已经存在),要做到这一点,但似乎很不成熟。
I can manage to do this with if statements (to check if a combination already exists), but that seems very unsophisticated.
有没有一种简单的方法,只使用循环,获得所有的可能性?
Is there a simple method, using only loops, to obtain all possibilities?
感谢您
编辑:由于这是下面提到的,不,这不是功课。这对于研究,以实现二进制状态的贝叶斯网络。 (开/关)。
Since this was mentioned below, no, this is not homework. This is for research in order to implement a Bayesian network of binary states. (on/off).
推荐答案
在Python中,使用和itertools 一>像这样的东西。
In Python, use itertools for stuff like this
from itertools import product
for i in product([0,1], repeat=5):
print i
收益率:
(0, 0, 0, 0, 0)
(0, 0, 0, 0, 1)
(0, 0, 0, 1, 0)
(0, 0, 0, 1, 1)
(0, 0, 1, 0, 0)
etc...
这篇关于我该如何计算的数字/位的阵列中所有的可能性(在python,或任何与此有关的语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!