mardi 22 janvier 2019

Pythonic way to create array with entries unique for row and column

I have a number of samples and want to pick a random subset of those of a defined length and repeat this process until every samples occurs 3 times, no sample occurring twice in a given row.

For example:

samples=range(12)
l=6
repeats=3

I expect 6 rows of 6 samples.

I tried the following but it only worked in one case (by chance) when the samples were picked equally, I usually get

ValueError: sample larger than population

import random
samples=range(12)
measured={key:0 for key in samples}
while len(samples)>0:  
    sample=random.sample(samples,6)
    print sample
    for s in sample:
        measured[s]+=1
        if measured[s]==3:
            samples.remove(s)

I was wondering if there is a way to tweek numpy.random.choice or from itertools.permutations but those didn't work because of the constrains above.

Is there a sample way I am overlooking or do I need to work with nested loops/ifs?




Aucun commentaire:

Enregistrer un commentaire