Good to all of you guys,
I'm "playing" a bit with the bitarrays in python and I have a problem and I do not know how to solve it, I commented:
I have a bitarray called aux composed of 6 zeros and a 1 such that '0000001'.
The idea is to list the combinations that exist by moving the 1 across the bitarray, that is, a list that is: {['0000001'], ['0000010'], ['0000100'], ['0001000' ], ['0010000'], ['0100000'], ['1000000']}.
The fact is that the modifications on aux to move the 1 to the left all the time works correctly but at the time of adding it to the list, it modifies all the values of the list instead of keeping them separate. I leave my code and screenshots for you to see. PS (I know that the algorithm does not work for any bitarray but I just need it to work for the example I mentioned)
Program Code:
Lista = []
aux = bitarray('00000001') #Creo el bitarray
Lista.append(aux) #Lo añado a la lista como primer elemento
#Muestro el elemento y la lista para ver que se ha añadido
#todo correctamente
print("La lista es: " , Lista)
print("Aux: " , aux)
while(aux[0] == False): #Bucle que se repite hasta que la primera
#Posicion del bitarray sea un 1
aux.remove(False) #Eliminamos la primera posicion
aux.append(False) #Insertamos un 0 al final para mover todo a la izquierda
print("*Aux despues del append: " , aux) #Mostramos aux despues de los cambios
Lista.append(aux) #Añadimos aux MODIFICADO a la lista
print("**La lista es: " , Lista) # Mostramos la lista
print("Aux despues de toda esta movida: " , aux) #Vemos como ha quedado aux
print("------------------------------------------------------------")
#Mostramos la lista posicion a posicion de forma detallada y visual
for i in range (len(Lista)):
print("Posicion ",i,": ",Lista[i])
And this is the output on the screen:
Thank you very much everyone and happy new year !!.