From getting started with python (DAY 2 ),

Source: Internet
Author: User

From getting started with python (DAY 2 ),

1. dictionary replication:

Dict = {'name': 'wang', 'sex': 'M', 'age': 34, 'job ': 'It'} info = dict # Alias (two dictionaries point to the same address space in the memory) info1 = dict. copy () # shadow copy (the first layer of the nested dictionary is independent, and the second layer is associated) import copycopy. copy () # shadow copy. deepcopy () # deep copy (completely independent)

Note: The associations in the light replication mode are only for the nested objects contained in the dictionary's initial state. Newly Added objects will not

Example:

>>> dict{'info': ['a', 'b', 1, 2], 'job': 'it', 'sex': 'm', 'age': 40, 'name': 'wang'}>>> dict_alias = dict>>> dict_copy = copy.copy(dict)>>> dict_deep = copy.deepcopy(dict)

# Adding, changing, and deleting the first-level object key values are not affected in light replication and deep Replication

>>> dict['age'] = 32>>> del dict['sex']>>> dict{'info': ['a', 'b', 1, 2], 'job': 'it', 'age': 32, 'name': 'wang'}>>> dict_alias   {'info': ['a', 'b', 1, 2], 'job': 'it', 'age': 32, 'name': 'wang'}>>> dict_copy   {'info': ['a', 'b', 1, 2], 'job': 'it', 'age': 40, 'name': 'wang', 'sex': 'm'}>>> dict_deep   {'info': ['a', 'b', 1, 2], 'job': 'it', 'age': 40, 'name': 'wang', 'sex': 'm'}

# Change or delete the original second-level object key value. The shortest copy is affected, but the deep copy is not affected.

>>> dict['info'][2] = 100>>> dict{'info': ['a', 'b', 100, 2], 'job': 'it', 'age': 32, 'name': 'wang'}>>> dict_alias{'info': ['a', 'b', 100, 2], 'job': 'it', 'age': 32, 'name': 'wang'}>>> dict_copy{'info': ['a', 'b', 100, 2], 'job': 'it', 'age': 40, 'name': 'wang', 'sex': 'm'}>>> dict_deep{'info': ['a', 'b', 1, 2], 'job': 'it', 'age': 40, 'name': 'wang', 'sex': 'm'}

# Adding second-level objects will not be affected in light replication and deep Replication

>>> dict['new'] = {'a':1, 'b':2, 'c':5}>>> dict{'info': ['a', 'b', 100, 2], 'name': 'wang', 'age': 32, 'job': 'it', 'new': {'a': 1, 'c': 5, 'b': 2}}>>> dict_alias{'info': ['a', 'b', 100, 2], 'name': 'wang', 'age': 32, 'job': 'it', 'new': {'a': 1, 'c': 5, 'b': 2}}>>> dict_copy{'info': ['a', 'b', 100, 2], 'job': 'it', 'age': 40, 'name': 'wang', 'sex': 'm'}>>> dict_deep{'info': ['a', 'b', 1, 2], 'job': 'it', 'age': 40, 'name': 'wang', 'sex': 'm'}

2. built-in function description:

_ Name __: the main file is returned; otherwise, the file name is returned, which can be used to determine whether the main file or the import module is used;

_ File __: absolute file path;

_ Doc __: description of the beginning of the file

Example:

'''  created by 2015-12-18  @author: kevin'''if __name__ == '__main__':  print('this is main file')  print(__file__)  print(__doc__)

Articles you may be interested in:
  • Python Dictionary operations
  • Comparison of Two Methods of traversing Dictionary (dict) in python
  • Dictionary details in python
  • Examples of conversion between dictionaries and JSON in Python
  • Python uses the dictionary dict to determine whether a specified key value exists
  • Use the update () method for dictionary operations in Python
  • Use the values () method to return the value of the dictionary key in Python
  • How to extract the dictionary key list using python
  • Python implements the exchange of dictionary keys and values

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.