Python full stack-Lesson 6 study notes

Python sixth lesson study notes

is id ==

  • is judges whether the memory addresses are the same
  • id gets the address in memory
  • == compares whether the values ​​on both sides are equal
  • Same id, same value
  • The same value, not necessarily the same id

Code block

  • Python programs are constructed from code blocks. A block is the text of a python program, which is executed as a unit. Code block: A module, a function, a class, a file, etc. are all a code block. Each command entered as an interactive method is a code block.
  • Code blocks: All our code depends on the code blocks for execution.
  • A file is a code block.
  • The next line of the interactive command is a block of code.

Two mechanisms (understand, no need to delve into)

  • Under the same code block, there is a mechanism. Under different code blocks, another mechanism is followed.

  • Cache mechanism within the same code block

    • Mechanism content: When Python executes the command of the initialization object of the same code block, it will check whether its value already exists, and if it exists, it will be reused. In other words: when executing the same code block, when encountering the command to initialize the object, he will store the initialized variable and value in a dictionary, and when he encounters a new variable, he will first query the record in the dictionary, If there is the same record then it will reuse the previous value in this dictionary. So in the example you gave, the two variables i1 and i2 will point to the same object when the file is executed (same code block). If the cache mechanism is satisfied, they only exist in memory, that is, the same id.
    • Object used: int bool str
    • Specific rules: all numbers, bool, almost all strings.
    • Advantages: Improve performance and save memory.
  • Cache mechanism under different code blocks: small data pool.

    • Mechanism content: Python automatically caches integers from -5 to 256. When you assign these integers to variables, you will not re-create the object, but use the cache object that has been created.

      python string will be certain rules in the string resides pool , create a copy, when you these strings assigned to the variable, and will not re-create the object, but used in a string resides in the pool is created Object.

        In fact, whether it is a cache or a string-resident pool, it is an optimization done by Python. It is to put an integer of ~ 5-256 and a certain string of rules in a 'pool' (container, or dictionary), No matter which variables in the program point to integers or strings in these ranges, then he is directly referenced in this 'pool', which implies that it creates one in memory.

    • Object used: int bool str

    • Specific rules: -5 ~ 256 numbers, bool, character string that meets certain rules.

    • Advantages: Improve performance and save memory.

Collection (less used)

  • Set: A container-type data type. It requires the elements inside to be immutable data, but it is itself a variable data type. The collection is unordered. {2, 3}.

  • The role of the collection:

    • Deduplication of the list.
    • Relationship test: intersection, union, difference, ...
  • Collection creation

  • #方式一(不常用)
    set1 = set({1,2,'jarvis',False})
    #方式二(一般使用)
    set1 = {1,2,'jarvis',False}
    #空集合
    set1 = set()
    set1 = {}    	#空字典
    
  • increase

  • set1 = {'one','two','three','four','five'}
    #方式一
    set1.add('six')
    #方式二(迭代着增加,分别增加a、b、c)
    set1.update('abc')
    
  • delete

  • set1 = {'one','two','three','four','five'}
    #方式一(remove,按照元素去删除)
    set1.remove('five')
    #方式二(pop,随机删除)
    set1.pop()
    
  • Change the value in disguise (first delete and then increase)

  • set1 = {'one','two','three','four','five'}
    set1.remove('one')
    set1.add('1')
    
  • Intersection operation

  • set1 = {1,2,3,4,5}
    set2 = {4,5,6,7,8}
    print(set1 & set2)
    
  • Union operation

  • set1 = {1,2,3,4,5}
    set2 = {4,5,6,7,8}
    print(set1 | set2)
    
  • Difference operation

  • set1 = {1,2,3,4,5}
    set2 = {4,5,6,7,8}
    print(set1 - set2)
    
  • Reverse intersection

  • set1 = {1,2,3,4,5}
    set2 = {4,5,6,7,8}
    print(set1 ^ set2)
    
  • Collection of children

  • set1 = {1,2,3,4,5}
    set2 = {1,2,3,4,5,6,7,8}
    print(set1 < set2)
    
  • Superset

  • set1 = {1,2,3,4,5}
    set2 = {1,2,3,4,5,6,7,8}
    print(set2 > set1)
    
  • List deduplication

  • ll = [1,1,2,2,2,3,3,3,4,5,6,6,6]
    set1 = set(ll)
    ll = list(set1)
    print(ll)
    
  • Relationship test

  • Uses: The relationship between data, the list is deduplicated.

Shallow copy

  • Assignment

  • l1 = [1,2,3,[4,5]]
    l2 = l1
    l1.append(6)
    print(l1)
    print(l2)
    
  • Shallow copy (list dict: nested variable data type is the same)

  • l1 = [1,2,3,[4,5]]
    l2 = l1.copy()
    l1.append(6)
    print(l1)
    print(l2)
    
  • l1 = [1,2,3,[4,5]]
    l2 = l1.copy()
    l1[-1].append(6)
    print(id(l1[-1]))
    print(id(l2[-1]))
    
  • Deep copy (Python has made an optimization for deep copy, using the same immutable data type.)

  • Summary: Deep copy will open up new space in memory, recreate the original list and the variable data types in the list, and the immutable data types will follow the previous ones.

  • import copy
    l1 = [1,2,3,[4,5]]
    l2 = copy.deepcopy(l1)
    l1[-1].append(6)
    print(l1)
    print(l2)
    

Guess you like

Origin www.cnblogs.com/rgz-blog/p/12693576.html