i have expected following:
a = [[1],[2],[3]] in a: = "x" print(a) to give:
["x","x","x"] but a remains unchanged. why this? have thought i pointer array. i? surely not copy of [1] regard first iteration of loop.
a = [[1],[2],[3]] in range(0, len(a)-1): a[i] = 'x' print(a) the reason because you're "picking out" copy of each object in array stand-alone item referred i in loop.
a = [[1],[2],[3]] copieditem in a: copieditem = 'x' # replaces copy picked out a. meaning you're replacing copy of item, not actual item in array/list. need replace indexing a[<index>] in order replace items in list.
another example this:
a = ['moo', 'cow', 'cat'] mycopy = a[0] # copies 'moo' 'mycopy' mycopy = 'the devil' # replaces 'moo'.. on copy.. print(a) ['moo', 'cow', 'cat'] print(mycopy) 'the devil' might bad example, same thing except loop iterating generator of list object returns each item asynchronous return called yield. each returned item copy , not reference initial object being same thing copying a[0] :)
Comments
Post a Comment