python basis day6

Day6 notes

一、id is ==

# id
# s = 'Jason'
# print(id(s))  # id的值是随机的
#
# l1 = [1, 2, 3]
# l2 = [1, 2, 3]
# print(l1 == l2)  # 结果为True,== 比较的是两边的值是否相等

# is 判断的是内存地址是否相同
l1 = [1, 2, 3]
l2 = [1, 2, 3]
print(l1 is l2)  # 结果为false,因为l1和l2分别创建了两个内存地址

s1 = 'Jason'
s2 = 'Jason'
print(s1 is s2)  # 结果为True,因为字符串只占一个内存地址

"""
id相同,值肯定相同;
值相同,id不一定相同
"""

Second, code block

Python program code block is constructed. Text block is a python program, which is executed as a unit.
Block: a module, a function, a class, a file, etc. is a block of code.
As the interaction (cmd into the python interpreter) is a command input for each block.

Two mechanisms: the same next block of code, there is a mechanism; different code blocks, follow another mechanism.

1. same next code block cache mechanism:

When the command to initialize the object encountered in the same block of code will initialize variables and values ​​in a dictionary, in the face of a new variable, first dictionary to find there the same value, if there is , the repeated use of the value.

  • Advantages: improve performance, save memory

  • Suitable

    int (float): any number in the same block of code are multiplexed.

    i1 = 1
    i2 = 1
    i3 = 1
    print(i1 is i2 is i3)
    # 结果为True
    

    str:

    1. String is not satisfied are obtained multiplication code block cache mechanism:
    s1 = '三上悠亚 aloha\t!@#*('
    s2 = '三上悠亚 aloha\t!@#*('
    s3 = '三上悠亚 aloha\t!@#*('
    print(s1 is s2 is s3)
    # 结果为True
    
    1. When the multiplier factor is 1, satisfying any string code block cache mechanism:
    s1 = '三上悠亚 aloha\t!@#*(' * 1
    s2 = '三上悠亚 aloha\t!@#*(' * 1
    s3 = '三上悠亚 aloha\t!@#*(' * 1
    print(s1 is s2 is s3)
    # 结果为True
    
    1. Multiplier> = 2: string contains only lowercase letters, numbers, underscores, multiplying by the total length of <= 20, to meet code block cache mechanism:
    s1 = '三上悠亚 aloha\t!@#*(' * 2
    s2 = '三上悠亚 aloha\t!@#*(' * 2
    s3 = '三上悠亚 aloha\t!@#*(' * 2
    print(s1 is s2 is s3)
    # 结果为False
    
    s1 = 'Aloha_' * 3
    s2 = 'Aloha_' * 3
    s3 = 'Aloha_' * 3
    print(s1 is s2 is s3)
    # 结果为True
    

    bool: True and False 1,0 way will exist in the dictionary, and reuse

2. The caching mechanism under different code blocks: small data pool

May be multiplexed digital strings of different code blocks from -5 to 256 and with a certain rule.
Advantages: improve performance, save memory

Third, the collection (set)

  • Collection is an iterative, disordered, the data structure can not contain duplicate elements

  • Data collection is a container type

  • Elements of the collection are immutable (hashable) element, and the collection itself is a variable data type.

    I.e., there is not set list (list), the dictionary (dict), a collection (set), can put a digital (int), a string (str), Boolean (bool)

  • The role of the collection: 1 deduplication List

    2. Relationship Test: intersection, union, difference

  1. Using the list to set the property to the weight, but can not maintain the original sequence
list1 = [1, 2, 2, 2, 2, 3, 3, 3, 'jason', 'jason']
set1 = set(list1)
list1 = list(set1)
print(list1)
  1. Set of CRUD
#集合的创建
set1 = {1, 3, 'jason', True, False}
print(set1)


set1 = {'jason', 'carly', 'aloha'}
# 增
# add()
set1.add('sb')
print(set1)
# update()迭代增加
set1.update('abc')
print(set1)

# 删
# remove()按照元素删除
set1.remove('aloha')
print(set1)
# pop()随机删除
set1.pop()
print(set1)
# clear()清空集合
set1.clear()
print(set1)
# del 删除集合
del set1

# 改(变相改值,先删再加)
  1. Collection intersection, union, difference, intersection Anti
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}

# 交集
print(set1 & set2)
print(set1.intersection(set2))

# 并集
print(set1 | set2)
print(set1.union(set2))

# 差集
print(set1 - set2)
print(set2 - set1)
print(set1.difference(set2))
print(set2.difference(set2))

# 反交集
print(set1 ^ set2)
print(set1.symmetric_difference(set2))
  1. Subset of the set, a superset
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5, 6}

# 子集
print(set1 < set2)
print(set1.issubset(set2))

# 超集
print(set2 > set1)
print(set2.issuperset(set1))

Fourth, copy depth

1. Assignment Operators

Like the assignment operator to make two lists memory address, that same id, so whether modifications list1, or modify list2, another list will change

list1 = [1, 2, 3, [11, 22, 33]]
list2 = list1
print(id(list1) == id(list2))
# 结果为True,此时list1 和list2 的id是一样的
list1.insert(-1, 666)
list2.pop(1)
list1[-1].append(888)
list2[-1].append(999)
print(list1)
print(list2)   # list1 和 list2 结果一样

2. shallow copy

list1 = [1, 2, [11, 22]]
list2 = list1.copy()
print(list1 == list2)  # True
print(id(list1) == id(list2))  # False
# 浅copy: 值相同,但是id不同

When list1 was created, it was created in memory 1, 2 and [11, 22] these three elements,
we can be understood as these three elements in a certain space in memory.
The id 11111111 list1 assumed, it is three slots pointing to the space 1,2, [11, 22]
At this time, the obtained copy list1 pale 22222222 id of the list2,
three slots are also list2 points to the space 1,2, [11, 22]

# 情况一、如果我们在list1这个列表进行修改,比如追加一个元素,删除一个元素,甚至是清空list1,仅仅是对id为11111111的list1进行了修改,而放在该空间中的1,2,[11, 22]并没有变化。
# 此时list2中的各个槽位指向的仍是该空间中的1,2,[11, 22],因此list2不会改变。
list1 = [1, 2, [11, 22]]
list2 = list1.copy()
list1.append(3)
print(list1)  # 结果为[1, 2, [11, 22], 3]
print(list2)  # 结果为[1, 2, [11, 22]]
# 情况二、如果我们对list1中的[11, 22]进行修改,此时对应在内存中的[11, 22]也会改变。
# 而list2中的[11, 22]也是指向内存中的[11, 22], 内存中的[11, 22]改变了,因此list2中的[11, 22]也随之改变
list1 = [1, 2, [11, 22]]
list2 = list1.copy()
list1[2].append(666)
print(list1)
print(list2)

3. deep copy need to import the copy module

import copy
list1 = [1, 2, [11, 22]]
list2 = copy.deepcopy(list1)
print(list1 == list2)
print(id(list1) == id(list2))
# 和浅copy一样,值相同,内存地址不同

list1 time is created is created in memory 2, [11, 22] of the three elements, as we can understand these three elements in a memory space which
is assumed list1 ID of 11111111, it is three slots pointing to a space inside the 1,2, [11, 22]
at this time, the obtained copy list1 depth of the id 22222222 List2, shallow copy is different, in this case a memory is created 1,2 , [11, 22] of the three elements, and in which two memory space
at this time are three slots list2 two points inside the space 2, [11, 22]
At this time, to modify the list1, no matter how modified, list2 is always the same, because list1 and list2 at this time there is no association

import copy
list1 = [1, 2, [11, 22]]
list2 = copy.deepcopy(list1)
list1.append(3)
list1[2].append(666)
print(list1)  # 结果为[1, 2, [11, 22, 666], 3]
print(list2)  # 结果为[1, 2, [11, 22]]

Note: The preceding appreciated that said two spaces are 1,2, [11, 22], which is the standard case of a deep copy.
But the python is optimized to save memory space, in python, int (float), str, bool data type that there is a immutable only in memory, because they are the same id.

Guess you like

Origin www.cnblogs.com/west-yang/p/12556540.html