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 also specified below:
#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 pick by draw from the list disciplines a value (cs
or gav
or icc
or comp
). I thought about using the method choice()
. After the value is chosen randomly, I must remove from disciplines and add in li
in a random position of li
, only where zero. I have to do this, as long as subjects are different from null.
OBS: I should add the value of disciplines only one in a row after the other, never isolated, so that it stays like this, for example:
li = [cs, cs, 2313, 1221, 0, 1333, 66, icc, icc, gav, gav]
I have already found a way to distribute in the positions where zero is found (in the example below, I replaced 0 by 1), so that only the item can be placed if the current item_and currentitem_+1 OR current item_and current item_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 very much!
Only where is one 0 followed by another right? Is it like your previous question? Whenever one is 0 followed by another 0 the two are changed to those values of disciplines
– Miguel
H+a a problem with "I must remove from disciplines and add in li", because if we remove the disc from the list disciplines every time we add one to l, then we will only be able to do this four times because the disciplines list will be empty at room 0 followed
– Miguel
Very correct Miguel, but no problem if only 4 loops are run in this case. The goal is this, because it is a genetic algorithm and this part that is being done is to generate an individual. I will make a list that stores all the individuals generated to generate the population :D
– Allan
Ha but taking into account the final result you want and published in the question
li = [cs, cs, 2313, 1221, 0, 1333, 66, icc, icc, gav, gav]
, this is not executed only 4 times– Miguel
True, but I have just given as an example to explain that they are dependent among themselves, for example: I can only add one discipline after another, but never in isolation. I just need when it’s randomly added to the list I read, it’s removed from the subjects, I don’t know if you can understand? Thank you very much! D
– Allan
Yeah, but think about me, this example you gave, there are six places where we need to do the right insertion? But in our disciplines only 4... What happened in the last 2 0 in
l
in a row?– Miguel
Perfect friend, excellent observation =) in my case, I did not put there not to get too much code, but I have actually a vector with 80 positions of zeros. And another vector disciplines with 50 positions. Will end up remaining place, but is that the very purpose :D has to do so? Already in the loop remove from disciplines?
– Allan
Of course, so the moment there are no more disciplines does not add more right? and will remove as you add right? I’m gonna make
– Miguel
Done at the bottom @Allan
– Miguel