Dec-22-2020, 03:46 AM
(This post was last modified: Dec-22-2020, 08:35 AM by Fernando_7obink.)
hello, is there someone have ever facing same situation like me ? lets say i have write a code as following :
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,
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...
, tq
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...
