Chained list: how to modify data from a list without changing the others?

Asked

Viewed 936 times

3

I have a for that generates a list of data lists

lista[[...], [...]]

Inside the "daughters" lists may or may not have other lists

lista[['dado11', 'dado12', [link1]],['dado21', 'dado22', [link2]]

This data I represented as link may or may not come within the list

lista[['dado11', 'dado12', []],['dado21', 'dado22', [link2], ['dado31', 'dado32', [link31, link32]]

However, when I use the go to pick up these "links", I just always feed last list... ie.. In the list[0] I always see the same lisks that exist in the list[2]..

I do not know if I could explain well, but I really wanted to know how to set the link list variable according to my need and that any change in this list did not influence the rest of the data already included in the mother list..

Summarizing my code goes something like this:

for linha in tmovimentos:
    texto = linha.text
    ...
    movi_anexos = []
    For link in links:
          movi_anexos.append([link.get_attribute('href'), 'out', '.pdf', data, 'tipo', texto])

    movimentacoes.append([data, texto, movi_anexos])

Thanks!

  • I didn’t quite understand, I would like to define values in lista[0] in the case of the first list of lists that appears, make the append of another list, which are links, is that it? It would be for example lista[0].append([link1, link2]), this way would have later lista[0] = ['dado11', 'dado12', [link1, link2]]

  • This, but for example, the movi_attachments always gives append in the links that exist, so every time I have a new list in the movement list, it brings me the attachments of the previous ones in the current one.. Understands?

2 answers

2

The only way to do this that you are describing here would be by iterating your list with one for and one for the list that is chained within the list.

a = [["a","a","a","a"],["a","a","a","a"]]
for lista in a:
    for item in lista:
        print("item")

To enter data in this list would be the same.

a = [["a","a","a","a"],["a","a","a","a"]]
for lista in a:
    lista.append(VALOR A DAR APPEND)

1

So -
what you want to do is confused How Voce is doing is confusing (you put a code "more or less like this", but it doesn’t have everything that is happening) - and you could have given some concrete explanation of how your data looks.

But, at last, the excerpt of code that you presented there shouldn’t present the problem you describe.

Your problem is this: you are inserting the same object (a list) as a list item in more than one place.

If you do it in Python:

In [86]: a = ["a", ]

In [87]: b = [a, a]

In [88]: b
Out[88]: [['a'], ['a']]


In [90]: a.append("b")

In [91]: b
Out[91]: [['a', 'b'], ['a', 'b']]

Note that within the list b the list a is present twice - is the same object. So when I change the list a, is expected to in b what I entered appears in both elements. No surprise there.

In your code, you must be doing this, directly or indirectly. But in the snippet that you pasted above, that doesn’t happen - you recreate the list movi_anexos as a new object, in each iteration of its first for, for example.

Python has a syntax that allows you to create a copy of a list by referencing it that may help you: When inserting a list as an item of another, instead of just the variable name, put that you want a slice (Slice) from the beginning to the end of the list being inserted (ask for the elements [:] a slice omitting the initial and final elements implies a slice from beginning to end) . List slices are copies of the original list, and a copy from beginning to end is nothing less than a copy of the list.

So if you write code like:] movimentacoes.append([data, texto, movi_anexos[:]) shall avoid that the same list is present in more than one position in its data structures. (but I emphasize that in the passage that is here this would not happen - but I do not know for example if your data and texto are lists too - if they are, give them the same treatment)

And finally, unrelated to your doubt: the way you’re trying to organize this data seems very problematic - you’re going to have a sopa of lists within lists, without defined length, without defined depth - it seems the worst nightmare to be able to recover anything later. Remember that there are also dictionaries, and above all, you can create your own classes that will gauge your information very well structured and can help you work with the data you have.

Browser other questions tagged

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