The Python for loop uses append () to add variable elements, the previous value is overwritten, and the memory application address is not changed in the loop

Source: Internet
Author: User

When using List.append (a) to add a dynamically changing a (A = Random.random ()), it is found that each new cyclic change of a in the loop changes the previous value in the list;

After the lookup, Python is based on the object reference, append added is an "address, reference", when the contents of this address changes, the previous same "address" content changes.

View "Memory, app" using ID (object).

Simple is not verbose on the online address memory has been introduced, after testing found that the general case of a new value of the variable when the ID will change, of course, if this value and the previous

The same ID is the same. This is not the case in the For loop.

Import Random def Test ():     return random.random () def test1 ():      for  in range (3):        as in range (2):             = Test ()             Print('a_id=', ID (a))

The result is

a_id= 2566513744848a_id= 2566513744872a_id= 2566513744848a_id= 2566513744872a_id = 2566513744848a_id= 2566513744872

You can see the memory allocation, corresponding to the inner layer 2 cycles to 2 2 addresses, save money; if so, use List.append () and end up with only 6 elements and 2 results.

But

Importrandomlist_a=[]list= [1,2,3,4,5,6]defTest ():returnrandom.random ()deftest1 (): forIinchRange (3):         forIinchRange (2): A=Test ()Print('a_id=', ID (a)) List_a.append (a) Print(list_a) test1 ()

Results

a_id= 2566513744824a_id= 2566513744848a_id= 2566513744968a_id= 2566513744992a_id = 2566513745016a_id= 2566513745040[0.5481244502902065, 0.7452961787111314, 0.6038274060224955, 0.8269310521431017, 0.4091898711994284, 0.45233748625853376]

You can see that the address has changed, and Python assigns new addresses and references at run time, so the preceding values do not change.

Said how much has not been to the point of the problem;

Importrandomlist_a=[]list= [[1,1,0,1,0], [1,0,1,1,1]]defmutation (Array): Array.append (Random.randint (1,10))    returnArraydeftest2 (): forNuminchlist: forIinchRange (3): A=mutation (NUM)Print(a)Print('a_id=', ID (a)) List_a.append (a)Print(list_a) test2 ()
[1, 1, 0, 1, 0, 8]a_id= 2566513576904[1, 1, 0, 1, 0, 8, 10]a_id= 2566513576904[1, 1, 0, 1, 0, 8, 10, 3]a_id= 2566513576904[1, 0, 1, 1, 1, 3]a_id= 2566515364744[1, 0, 1, 1, 1, 3, 5]a_id= 2566515364744[1, 0, 1, 1, 1, 3, 5, 4]a_id= 2566515364744[[1, 1, 0, 1, 0, 8, 10, 3], [1, 1, 0, 1, 0, 8, 10, 3], [1, 1, 0, 1, 0, 8, 10, 3], [1, 0, 1, 1, 1, 3, 5, 4], [1, 0, 1, 1, 1, 3, 5, 4], [1, 0, 1, 1, 1, 3, 5, 4]

The result is that I added a new value to the list, but the change of the element in the list does not change the IDof the list, and the ID of the list in the 3 loop of the inner layer is always the same.

In the 3 loops that are added to the inner layer of list_a, it is always an ID, and the last value of this ID is taken after the outer first loop ends, so the last 3 values of the list_a are the same.

In fact, a list on the web in the dictionary example and this is the same, the double-loop memory loop does not change the ID of the object,

It's just that when I write the list into other forms like ([Sum (list),,,]), look for the reason when the ID and the results of the conversion found that a lot of results are the same,

By the other many guess the wrong try a lot, find a long time to find the reason.

The solution to this problem is to use copy,

Mutation (array) in copy to return the array to produce a new ID, (array = copy.copy (array)),

The Python for loop uses append () to add variable elements, the previous value is overwritten, and the memory application address is not changed in the loop

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.