Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
About list and method ....
#1
hello, is there someone have ever facing same situation like me ? lets say i have write a code as following :

level_1 = []
level_2 = []

for t in range(2):

    for f in range(2):
        level_0 = ["x" for r in range(3)]
        print("level_0 =",level_0)
        level_1.append(level_0)
        print("level_1 =",level_1)
        
    level_2.append(level_1)
    print("level_2 =",level_2)
and the output become:
level_2 =[[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']], [['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]]
my question is :
why the output level_2 become 2 x 4 x 3 element ?

Because from my thought is like this :

-for level_0 for-loop it will fill the level_0 list with element 'x' each time the for level_0 command is executed, because the loop-for require 3x looping (in range 3) so the list level_0 becomes level_0 = ['x', 'x', 'x']
-and the program execute next command which is level_1.append(level_0) that means every time this command executed, the level_1 list will fill with an element level_0 that already contain 3x element "x" before, because the for-loop on level_1 require 2x (in range 2) looping then level_1 list becomes, level_1 = [['x', 'x', 'x'], ['x', 'x', 'x']] and program will break off from the loop
-next command is level_2.append(level_1), that means level_2 list will be filled by the level_1 list as an object/element, then the level_2 list will becomes like this : list_2 = [[['x', 'x', 'x'], ['x', 'x', 'x']]]
-Because the for-loop level_2 require 2x looping so the program heading back to the top and do the for-loop level_1 and level_0 once more

-level_0 list will be refill again with 3 element "x" and become level_0 = ["x","x","x"]
-and the program will fill the level_1 list that already have 2 object/element level_1 before, the loop require 2x (in range 2) looping so the list will become, level_1 = [['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']] === > level 1 list that already contain 2 element/object will be added by 2 more element
-last the level_1 list will be added to level_2 list that already contain 1 element before, so the end output will become,

level_2 = [[['x', 'x', 'x'], ['x', 'x', 'x']],[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]]
=== > this is from my thoughts, but the Python output is :

level_2 = [[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']], [['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]]
can someone explain to me why my analysis is wrong ?

Note : Sorry if you get confuse from my writing because the lack of my English language ability, but i hope you can understand what the point of my question is... Cool , tq
Reply
#2
When you repeatedly do level2.append(level1), you keep putting another copy of the same list into the outer list.

Then when you modify level1, all the copies inside are changed. If you want them independent, you need to make different copies.

>>> inner = ["x"]
>>> outer = [inner]
>>> inner.append("z")  # Modifies the object that is inside "outer"
>>> outer
[['x', 'z']]
>>> inner = ["x"]
>>> outer = [inner[:]]  # contents are a copy of inner, not the same object
>>> inner.append("z")   # modifies inner, but not the object in outer
>>> inner
['x', 'z']
>>> outer
[['x']]
Fernando_7obink likes this post
Reply
#3
You need to surround code in Python tags or the indentation is lost. I think your code does this:
level_1 = []
level_2 = []

for t in range(2):
    for f in range(2):
        level_0 = ["x" for r in range(3)]
        level_1.append(level_0)
    level_2.append(level_1)
    
print(level_2)
This code makes four level_0's and puts them in level_1.
for t in range(2):
    for f in range(2):
        level_0 = ["x" for r in range(3)] # executed 4 times
        level_1.append(level_0)
This code puts 2 level_1's in level_2:
for t in range(2):
    ...snip...
    level_2.append(level_1) # executed twice
This code does the same thing.
level_1 = []
level_2 = []

for t in range(4):
    level_0 = ['x']*3
    level_1.append(level_0)
    
for f in range(2):
    level_2.append(level_1)

print(level_2)
The code could not be refactored this way if the loops didn't keep appending to level_1 and level_2.
Reply
#4
(Dec-22-2020, 04:02 AM)bowlofred Wrote: When you repeatedly do level2.append(level1), you keep putting another copy of the same list into the outer list.

Then when you modify level1, all the copies inside are changed. If you want them independent, you need to make different copies.

>>> inner = ["x"]
>>> outer = [inner]
>>> inner.append("z")  # Modifies the object that is inside "outer"
>>> outer
[['x', 'z']]
>>> inner = ["x"]
>>> outer = [inner[:]]  # contents are a copy of inner, not the same object
>>> inner.append("z")   # modifies inner, but not the object in outer
>>> inner
['x', 'z']
>>> outer
[['x']]

Yes, it seems you right,

because from my analysis after read your answer my code working looks like this :
-when the first loop level_2 done, the level_2 list contain as following :
list_2 = [[['x', 'x', 'x'], ['x', 'x', 'x']]]
then if i follow your answer, when Python execute level_1.append(level_0) for the second loop level_2 :
the level_1 list that already built like this
level_1 = [['x', 'x', 'x'], ['x', 'x', 'x']]
will append by a level_0 list
level_0 = ['x', 'x', 'x']
but because the inside of level_2 is a copy from level_1 then the level_2 list will do the append methode too by adding the level_0 list and the level_2 list become like this :
level_1 = [['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]
level_2 = [[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]]
because the loop in range(2), so the Program repeat the command again, then the list become like this :
level_1 = [['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]
level_2 = [[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]]
after finish the level_1 loop then the program execute the level_2.append(level_1) command so the level_2 list will be added by the level_1 list that already contain 4 element level_0, then the level_2 list become like this :
level_2 = [[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']], [['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]]
i think i must be more aware with the list instruction because its different from executing a variable, i think i need to learn more Tongue, tq for your help...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question about List.reverse() method tomliuwhite 1 1,303 Dec-07-2021, 08:20 AM
Last Post: ndc85430
  Unable to use random.choice(list) in async method spacedog 4 3,354 Apr-29-2021, 04:08 PM
Last Post: spacedog
  print all method and property of list object engmoh 4 2,799 Oct-26-2019, 05:33 PM
Last Post: engmoh
  why my method doesn't find my List in the same class? Scorpio 2 2,354 Jan-31-2019, 05:21 PM
Last Post: Scorpio
  [Python Class] save method output to global file/list Vijay 3 4,980 Dec-23-2017, 03:20 AM
Last Post: Vijay

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020