Iterations in a tuple list

Asked

Viewed 69 times

-1

Good afternoon,

I have the following problem:

I have a list of tuples called list_chromosomes, with 10 positions.

I want to open a new position at the end of it (11th position), where it will initially equal the first. For that I used

lista_cromossomos.append.(lista_cromossomos[0])

Upshot:

[[1, 0, 0, 0], .. , [1, 0, 0, 0]] -> 11 terms, where the first and the last are equal

I want to multiple only the components of the last term by 7 to stay [7, 0, 0, 0], so the expected is:

Expected result:

[[1, 0, 0, 0], .. , [7, 0, 0]]

For that I used:

 for y in range(0, len(lista_cromossomos[10])):
        lista_cromossomos[10][y] = lista_cromossomos[10][y] * 7

It turns out that instead of multiple just the last tuple, it multiple the last and the first.

Code result:

[[7, 0, 0, 0], .. , [7, 0, 0]] -

What’s wrong with it?

2 answers

1

Code to do what you want:

#declarar variaveis
tuplo = (1,0,0,0)
lista_de_10tuplos = []

#criar lista de 10 tuplos
for i in range(0,10):
    lista_de_10tuplos += [tuplo,]
    
print (lista_de_10tuplos)

#adicionar um tuplo
lista_de11_tuplos = lista_de_10tuplos + [tuplo,]

#criar tuplo modificado
tuplo_modificado = (tuplo[0]*7, tuplo[1], tuplo[2], tuplo[3])
#nota: seria mais simples ecrever tuplo_modificado = (7,0,0,0) mas assim 
#o codigo nao faria uma multiplicacao

#substituir ultimo tuplo por tuplo modificado
lista_de11_tuplos[10]=tuplo_modificado

print (lista_de11_tuplos)

Feedback I got from the prints:

[(1, 0, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0, 0), (1, 0, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0)]

[(1, 0, 0, 0), (1, 0, 0), (1, 0, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0, 0), (7, 0, 0, 0, 0)]

0


First of all you should know that when you create a list or any other object, you do not assign to the variable the object itself but rather a memory address of the object that is stored inside your RAM memory.

Just when we create a code of this kind:

lista1 = [1,0,0,0]
lista2 = lista1

We are not creating a new object of list() to the lista2, we just passed the memory address of the lista1. I mean, both lista1 as lista2 point to the same object in memory.

This is exactly why when you change the values of the final position list, the values of the first position list are also changed.

And how do we solve the problem ? Just create a new object using one of these two forms:

# O método copy() retorna um novo objeto com os mesmos valores.
lista_cromossomos.append(lista_cromossomos[0].copy()) 

# Abaixo nós obtemos uma nova lista através de slice.
lista_cromossomos.append(lista_cromossomos[0][:]) 
  • Very good, partially solved. In the complete case it is a class, where the first attribute is this "chromosome", and the second is a float number. When I use this in the float the following error appears: 'float' Object has no attribute 'copy' I did so: 
classe[10].cromossomo =classe[0].cromossomo.copy -> Funciona
classe[10].numero = classe[x].numero.copy -> Não funciona
 You know why?

  • The method copy() exists only for objects of list(), dict() and set() (may exist for other objects). What I said about "memory addresses" is not applied to values of primitive types, so unlike the example I showed in the answer, when making the code inteiro2 = inteiro1, a new value would be created and assigned to the variable. Therefore, there is no reason to have a method copy() for primitive types.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.