Random distribution in lists with python

3

I have a list:

li = [0, 0, 2313, 1221, 0, 1333, 66, 0, 0, 0, 0]  

and another list of lists, where:

The list of lists is called disciplines (below). The lists that are within disciplines, are specified below also:

#essas são as listas que estão dentro de disciplinas:
cs = [2313, 2214, 2120]
gav = [1101, 1103, 1104]
icc = [3201, 3304, 3209]
comp = [4101, 4203, 4409, 4559]

#lista de listas
disciplinas = [cs, gav, icc, comp]

I need to sort by a draw from the disciplines list a value ( cs or gav or icc or comp ). I thought about using the choice() method. After the value is chosen randomly, I must remove it from disciplines and add in li in a random position of li , only where zero. I have to do this while disciplines are different from zero.

OBS: I should add the value of disciplines only one followed by another, never isolated, so that it looks like this:

li = [cs, cs, 2313, 1221, 0, 1333, 66, icc, icc, gav, gav]  

I've already been able to find a way to distribute in positions where I find zero (in the example below, I substituted 0 for 1), so that I can only place the item if the current_table and current_table + 1 OR current_table and current_table_1 are equal to zero:

l = [0, 0, 2313, 1221, 0, 1333, 66, 0, 0, 0, 0]
lCount = len(l)
next1 = False
for i in range(0, lCount-1):
    if(l[i] == 0 and l[i+1] == 0):
        l[i] = 1
        l[i+1] = 1
        next1 = True
    if(next1): # ultimo elemento caso seja 0 seguido de outro (next1 definido no ultimo loop do ciclo)
        l[-1] = 1
print(l) # [1, 1, 2313, 1221, 0, 1333, 66, 1, 1, 1, 1]

Thank you!

    
asked by anonymous 19.06.2016 / 04:28

2 answers

2

Exactly with choice you have to do, you must import the random module:

import random

cs = [2313, 2214, 2120]
gav = [1101, 1103, 1104]
icc = [3201, 3304, 3209]
comp = [4101, 4203, 4409, 4559]

disciplinas = [cs, gav, icc, comp]
l = [0, 0, 2313, 1221, 0, 1333, 66, 0, 0, 0, 0]

randDisc = [] # armazenamos os resultados sorteados para mais tarde remove-los da lista disciplinas
lCount = len(l)
next1 = False
for i in range(0, lCount-1):
    if(len(disciplinas) > 0):
        disc = random.choice(disciplinas)
        if(l[i] == 0 and l[i+1] == 0):
            l[i] = disc
            randDisc.append(disc)
            disciplinas.remove(disc)
            next1 = True
        elif(next1):
            l[i] = disc
            randDisc.append(disc)
            disciplinas.remove(disc)
            next1 = False
    else:
        break

print(l) # [[1101, 1103, 1104], [2313, 2214, 2120], 2313, 1221, 0, 1333, 66, [3201, 3304, 3209], [4101, 4203, 4409, 4559], 0, 0]
print(disciplinas) # []
    
19.06.2016 / 09:41
1

You can use random to draw the value and the new position to insert. If the position is different from 0 , try again.

import random as r
li = [None, None, 65123, 2223, 4536, None, None, None]

while(len(disciplina) > 0):
    p_rem = r.randint(0, len(disciplina)-1)
    v_rem = disciplina[p_rem]
    disciplina.remove(v_rem)

    while True:
        li_add = r.randint(0, len(li)-1)
        if not li[li_add]:
            li[li_add] = v_rem
            break
    
19.06.2016 / 05:37