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

2

I have a for which generates a list of data list

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

Within "daughter" lists, you may or may not have other lists

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

This data that I represented as a link may or may not be included in the list

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

However, when I use for to get these links, I just always feed the last list ... That is .. In the list [0] I always see the same lisks that exist in the list [2]. .

I do not know if I could explain it well, but I really wanted to know how to set the variable list of links according to my need and that some change in that list would not influence the rest of the data already included in the parent list ..

Summarizing my code looks 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])

Thank you!

    
asked by anonymous 11.11.2016 / 19:26

2 answers

1

The only way to do this that you are describing here would be you iterating your list with a for and another 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 do the insertion of data in this list would be the same way.

a = [["a","a","a","a"],["a","a","a","a"]]
for lista in a:
    lista.append(VALOR A DAR APPEND)
    
14.11.2016 / 01:02
0

Then -
What you want to do is confusing. How you are doing is confusing (you put a "like this" code but it does not have everything that is going on) - and you could have given some concrete example of how your data gets.

But lastly, the code snippet you should not present the problem you describe.

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

If you do this 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']]

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

In your code, you should be doing this, either directly or indirectly. But in the snippet you pasted above, this does not happen - you recreate the movi_anexos list as a new object, for each iteration of your first for , for example.

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

So if you write code like:] movimentacoes.append([data, texto, movi_anexos[:]) should prevent the same list from being present in more than one position in its data structures. (but I emphasize that in the section 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'll get a% of lists within lists, with no defined length, no defined depth - looks like 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 manage your information very well structured and can help you to work with the data you have.

    
11.11.2016 / 21:12