Small depth data set copy pool

Small depth data set copy pool

A small pool of data

  • Definition: Python small data pool is to improve the efficiency of one way, using the same type of fixed data memory address, data type support: str, int, bool is a caching mechanism, it referred to as Patent resident mechanism, the major program languages ​​have similar things.

  • id () ----- see the elements of the memory address
  • The difference is with ==
    • two elements is determined whether the memory address is equal to
    • == determination value approximately equal sign elements on both sides are equal
  • Small data pool rules
    • Digital: -5 to 256
    • String:
      • Multiply string (not a multiplier) when the total length can not exceed 20
      • Unrestricted length string of their definition, the string must consist of letters, numbers, underscores.
      • Special characters (except for Chinese) defines a time, to camp
  • Block: a file, a module, a function, a class, each row is a terminal block
    • rule
      • Digital: all reside
      • String:
        1. Their full-defined string resides
        2. The total length of no more than 20 strings are multiplied (multiplier is not 1)
        3. Python3.7 above multiplication when the total length can not exceed 4096

Two, set collection

  • Definition: set value is not a dictionary, disorder, de-emphasis, hash, it is a set of variable data type

  • Syntax: {1,2,3,4,5}

  • A collection of related operations

    s = {1,2,3,4,5}

    • increase
      s.update(可迭代数据类型)   ---  迭代添加
      s.add('haven')      -----   单个添加
    • delete
      # s.pop()       ---- 随机删除
      # s.remove(3)   ---- 指定元素删除
      # s.clear()     ---- 清空(注:结果为set(),为了与字典区分
      # del s         ---- 删除整个集合
    • change

      It can only be deleted together

    • check

      for loop iterates

    • Other operations
      s1 = {1,2,3,4}
      s2 = {3,4,5,6}
      print(s1-s2)  --- 差集 1,2
      print(s1|s2)  --- 并集 1,2,3,4,5,6
      print(s1&s2)  --- 交集 3,4
      print(s1^s2)  --- 反交集 1,2,5,6
      print(s1>s2)  --- 判断s1是否为s2的父集  返回布尔值
      print(s1<s2)  --- 判断s1是否为s2的子集  返回布尔值
      s3 = frozenset({1,2,3,4,5,6}) --- 冻结集合,变成不可变数据类型
    • Key uses a set of de-emphasis ---
      lis = [1,2,2,3,2,4,5,6,5,6,7]
      print(list(set(lis)))  --- 结果[1, 2, 3, 4, 5, 6, 7]  去重是数组最重要的用法

Third, the depth of copies

  • Assignment =

    No assignment to create a new space, it is multiple variables point to the same memory address

  • Shallow copy
    • l2 = l1 [:] and l2 = l1.copy ()
    • Only copy of the first layer element
    • Creates a new container, container elements and container elements in the original point to the same memory address
  • Deep copy
    • import copy

      l2 = copy.deepcopy(l1)

    • Immutable data types of the original data point to the same space

    • Variable data types will create a new space

  • to sum up:
    • Shallow copy only create new space for the outermost layer of variable data types
    • Deep copy creates a new space for each layer variable data types

Guess you like

Origin www.cnblogs.com/douzi-m/p/11904738.html