List of bitarray in python

1

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 !!.

    
asked by Josemanuu 02.01.2019 в 13:36
source

1 answer

1

Your problem is that you are always using the same variable aux that you modify and put in the list.

Since in Python all the variables are actually references to objects, when you make Lista.append(aux) what you put in that list is a reference to the variable aux . If you then change the value of that variable, although the reference still points to the same place, the content will have changed.

To make it look simpler:

lista = []
aux = bitarray('00000001')
lista.append(aux)
aux.append(True)
print(aux)
print(lista)
bitarray('000000011')
[bitarray('000000011')]

What you have to do is create a new bitarray that is a copy of aux . The references will then be different, and thus the original variable that was already on the list will not be modified:

lista = []
aux = bitarray('00000001')
lista.append(aux)
aux = aux.copy()
aux.append(True)
print(aux)
print(lista)
bitarray('000000011')
[bitarray('00000001')]

In your case, it would be enough to take this copy at the beginning of the loop:

while(aux[0] == False): #Bucle que se repite hasta que la primera 
    aux = aux.copy()   # <----- AÑADIR ESTO
    #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
    
answered by 02.01.2019 / 14:14
source