Doubt simple list of lists in Python

1

I'm starting with Python , I was trying to edit a list, but I discovered that I could not do it, since the strings are immutable . I found this solution:

lista = [0, 0, 0, 0, 0]

lista_temp = []
a=3
b="c"

for i in range(0,len(lista)):
    if i==a:
        lista_temp.append(b)

    else:
        lista_temp.append(lista[i])

print(lista_temp)

Result of printing:

  

[0,0,0, "c", 0]

I would like to be able to do the same with a list of lists, that is, than this:

lista=[[[0,0,0],[0,0,0]],[[0,0,0],[0,0,0]]]

... happen to be this:

lista_temp=[[[0,0,0],[0,0,0]],[["c",0,0],[0,0,0]]]
    
asked by Don Felipe 20.05.2017 в 23:12
source

2 answers

0

To do that it is not necessary to complicate your life with three for nested (if there is no cause that forces you to do so), use copy.deepcopy() from the standard library that makes a 'dead' copy of the object you want, then you use the indexes to change the values in the copy:

from copy import deepcopy

lista=[[[0,0,0],[0,0,0]],[[0,0,0],[0,0,0]]]

x = 1
y = 0
z = 0
c = 'c'

lista_temp = deepcopy(lista)
lista_temp[x][y][z] = c

Keep in mind that you have a three-dimensional matrix, you need to specify the coordinates in three axes ( x , y , z ) to assign the value.

    
answered by 20.05.2017 / 23:32
source
0

Well, first things first: the "lists" go between [] and the tuples go between () ... a tuple is immutable, a list can be modified instead.

therefore the following is NOT immutable (can be modified):

lista = [0, 0, 0, 0, 0]

To be immutable it should be replaced (for example):

secuencia = (0, 0, 0, 0, 0)

In this context, if it makes sense to talk about building lista_temp . otherwise we would simply modify lista . Apart from this clarification the following procedure is analogous

Python and many languages (which incorporate certain features of the functional paradigm) incorporate "slicing" that is, you can cut strings with indices, in turn copy their content into other elements, referencing them correctly:

>>>lista = [0, 0, 0, 0, 0]
>>>lista_temp = lista[:3] + list("c") + lista[4:]
>>> lista_temp
[0, 0, 0, 'c', 0]

It is not necessary to make any loop for , this would be with respect to the first case. On the other hand in the example that is shown later there is a problem regarding the dimensionality (or nesting) of the lists:

lista=[[[0,0,0],[0,0,0]],[[0,0,0],[0,0,0]]]

has two elements (this would look if len(lista) is executed). therefore in lista_temp=[[[0,0,0],[0,0,0]],[["c",0,0],[0,0,0]]] the second element of the list was modified (better said the first element of the second element of the main list), which is NOT the same as that shown in the first example where the fourth element is modified. (Keep in mind that the indexes start from zero therefore when the conditional checks that a=3 , is checking that it is in the fourth element)

Anyway, here I put what would be done using slicing (so that it is as in the second example):

>>> lista=[[[0,0,0],[0,0,0]],[[0,0,0],[0,0,0]]]
>>> lista_temp = lista[:1] + [ ["c",0,0] + lista[1][1:]]
>>> lista_temp
[[[0, 0, 0], [0, 0, 0]], ['c', 0, 0, [0, 0, 0]]]
    
answered by 21.05.2017 в 22:50