Home > other >  Small white difficulties, hope to get your hands
Small white difficulties, hope to get your hands

Time:09-18

The younger brother in a problem encountered in the development process, directly on the code:
Company=[]
Company_temp=[]

Def company () :
Global Companys
Global Companys_temp
Read the database, the data, assuming that reads data for [' 1 ' '2' '3', '4'], and to assign values to Company_temp
Print (Company_temp)
Print (Company)
Company=Company_temp
Company_temp. Append (' a ')
Print (Company_temp)
Print (Company)



Results: [' 1 ', '2', '3', '4']
[]
[' 1 ', '2', '3', '4', 'a']
[' 1 ', '2', '3', '4', 'a']
I'm not in the Company, 'a' is added to the array output but why will have this item? Thank you for guidance

CodePudding user response:

You can print the two list address to see see

CodePudding user response:

Is to point to the same address, is the fault of the assignment of? List two point to the same address, change one of the list of values, another with changed

CodePudding user response:

Python list, assign a value to use the method of slices copy

Company=Company_temp [:]

CodePudding user response:

Copy of the depth problem

Company_temp=[' 1 ', '2', '3', '4']
Company=company_temp. Copy ()
Company_temp. Append (' a ')
Print (company_temp)
Print (company)

CodePudding user response:

Company=Company_temp don't mean to put Company_temp value is assigned to the Company, but the Company refers to the memory address Company_temp, when Company_temp. Append (' a '), in their own back to the memory address of 'a' is added, so the Company will read Company_temp stored in its memory address values,
  • Related