Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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列表对二进制变量执行2的运行长度控制_Python_Python 2.7 - Fatal编程技术网

使用python列表对二进制变量执行2的运行长度控制

使用python列表对二进制变量执行2的运行长度控制,python,python-2.7,Python,Python 2.7,我想伪随机地创建一个包含48个条目的列表——24个0和24个1——其中0和1在一行中的重复次数都不超过两次。例如: [1,1,0,1,0,0,1,1,0,1,0,...] import random l = list() for i in range(48): if len(l) < 2: l.append(random.choice([0,1])) else: if l[i-1] == l[i-2]: if l[i

我想伪随机地创建一个包含48个条目的列表——24个0和24个1——其中0和1在一行中的重复次数都不超过两次。例如:

[1,1,0,1,0,0,1,1,0,1,0,...]

import random
l = list()
for i in range(48):
    if len(l) < 2:
        l.append(random.choice([0,1]))
    else:
        if l[i-1] == l[i-2]:
            if l[i-1] == 0:
                l.append(1)
            else:
                l.append(0)
        else:
            l.append(random.choice([0,1]))
[1,1,0,1,0,0,1,1,0,1,0,…]
随机输入
l=列表()
对于范围(48)内的i:
如果len(l)<2:
l、 追加(随机选择([0,1]))
其他:
如果l[i-1]==l[i-2]:
如果l[i-1]==0:
l、 附加(1)
其他:
l、 追加(0)
其他:
l、 追加(随机选择([0,1]))

我有上面的代码,但它有时返回1或0的不均匀计数。所有可能的解决方案都应该有相同的出现机会

这里有一个函数,它接受一个整数
n
,并返回一个包含
n
0s和
n
1s的列表

它首先随机选择整数0或1中的一个,然后通过随机选择一个位置并检查在该位置插入整数是否违反约束,从而遵守“行中3号约束”。如果没有,它会将其插入其中,否则它会随机选择一个不同的位置进行尝试。它一直持续,直到所有
n
0和所有
n
1都被放在列表中的某个位置

它通过在0和1被插入到列表中后减少每个0和1的计数器来跟踪到目前为止使用了多少0和1。如果没有更多的0可添加,它将添加其余的1(反之亦然)

当列表长度达到
2*n
时,函数返回列表(所有0和1都已用完)


简单的解决方案:

import random

def gen_list():
    bin_list=[]
    count=(0,0)
    while len(bin_list)<48:
        choice=random.randint(0,1)
        if choice==0:
            bin_list.append(0)
            count=(count[0]+1,count[1])
        else:
            bin_list.append(1)
            count=(count[0],count[1]+1)
        violates_run_constraint=(len(bin_list)>3) and (bin_list[-1]==bin_list[-2]==bin_list[-3])
        if violates_run_constraint:
            return ([],())
    return (bin_list,count)


bin_list,count=gen_list()
while(bin_list==[] or count!=(24,24)):
    bin_list,count=gen_list()
print(bin_list,count)
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1] (24, 24)

[1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0] (24, 24)

@cricket_007我同意原来的问题是一个dup,但他新编辑的版本有原创性的元素,这是由他使用的术语“伪随机”提出的。我不确定这个问题的措辞是否正确(编辑后)这个序列的一个副本被标记了。为什么它被标记为dupe?@jtm_beta:我的意思是这样的序列数量有限,比如说N。你需要每个序列都有1/N的机会出现吗?或者如果它是一个骰子,你会同意1,2,4比3,5和6更频繁地出现吗?DSM:每个可能的解决方案都有1/N的机会出现
import random

def gen_list():
    bin_list=[]
    count=(0,0)
    while len(bin_list)<48:
        choice=random.randint(0,1)
        if choice==0:
            bin_list.append(0)
            count=(count[0]+1,count[1])
        else:
            bin_list.append(1)
            count=(count[0],count[1]+1)
        violates_run_constraint=(len(bin_list)>3) and (bin_list[-1]==bin_list[-2]==bin_list[-3])
        if violates_run_constraint:
            return ([],())
    return (bin_list,count)


bin_list,count=gen_list()
while(bin_list==[] or count!=(24,24)):
    bin_list,count=gen_list()
print(bin_list,count)
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1] (24, 24)

[1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0] (24, 24)