Chapter VI python shades of copies (BASIC)
6.1 small data pool
Python is a small data pool of one way to improve efficiency, fixed data using the same memory address type
Decimal pool - support: str, int, bool
1 = A
b = 1
the above mentioned id View memory address space Gets An address open space
print(id(a))
print(id(b))
Digital support small data pool range: -5 to 256 * Remember
a = 300
b = 300
print(id(a))
print(id(b))
Block: a file, a module, a function, a class, a terminal block in each row
in python dictionary is stored
a = 1000
b = 1000
print(id(a))
print(id(b))
determining whether the two memory address is # # must be remembered that identical
== # determines whether the same value equal sign must remember #
String:
1. String doing multiplication when the total length can not exceed 20, a resident # Remember
the string length 2. The definition does not limit himself string must (letters, numbers, underscores.) Composition, to camp
3 special characters (except Chinese) defined a time, to camp
4. The string is actually assigned * 1
If the determination is the same == necessarily the same
if the same is not necessarily the same determination ==
a = 1000
b = 1000
print(a == b)
a = "alexdsb" * 2
b = "alexdsb" * 2
print(a is b)
a = "12345678912345678qwertyuioqwertyuioQWERTYUIOWERTYUIOQWERTYUIOasdfghjksdfghjk____asdgadsfasdfgasdgasdgfsdaf"
b = "12345678912345678qwertyuioqwertyuioQWERTYUIOWERTYUIOQWERTYUIOasdfghjksdfghjk____asdgadsfasdfgasdgasdgfsdaf"
print(a is b)
a = "你"
b = "你"
print(a is b) # pycharm中是True 因为执行了代码块,终端中是False
a = "@"
b = "@"
print(a is b)
a = "你好"
b = a * 1
print(a is b)
先执行代码块,不符合代码块才会执行小数据
Rule code block:
number: all reside
string:
1. multiply the character when the total length can not exceed 20
2. Custom defined reside
3. The multiplier of 1 is assigned when
4.python3.7 multiplication when the total length can not exceed 4096
Summary:
Decimal pool - support: str, int, bool
small pool of digital data: -5 to 256
small cell string data: multiplying the length of time can not exceed 20
6.2 set collection
Collection presentation
set is a collection of python ⼀ a basic data types: random variable.
Use generally not very often used. SET is element is not repeated. Disorder. Element surface must be a hash (int, str, tuple, bool), we can come to mind. SET is a data type dict without saving value, save only the key. set is also displayed in table use shown by {}
s = {1,2,3,"123",False,(1,2,3,4)}
set collection increase
s.update iterative added
s = {1,2,3,"123",False,(1,2,3,4)}
s.update("3456") # 迭代添加,且无序
print(s)
{False, 1, 2, 3, '4', (1, 2, 3, 4), '3', '123', '5', '6'}
s.add add, duplicate content is not added
s = {1,2,3,"123",False,(1,2,3,4),"玲玲"}
s.add("玲玲")
print(s)
s.add("玲玲") # 重复的内容不不会被添加到set集合中
print(s)
To delete a collection set
s = {1,2,3,"123",False,(1,2,3,4),"玲玲"}
s.pop() #随机删除
s.remove(3) #直接删除元素,不存在这个元素. 删除会报错
s.clear() # 清空set集合.需要注意的是set集合如果是空的.,打印出来是set()因为要和 dict区分set()
del s #删除整个集合
set collection of change
Data set collection is not indexed. ⼀ there is no way to locate an element, so there is no way to directly modify trekking into ⾏. Can only be recorded using a ⽅ way to add after deleting the first to complete the modification operation
s = {1,2,3,"123",False,(1,2,3,4),"玲玲"} # 把玲玲改成毛毛
s.remove("玲玲")
s.add("毛毛")
print(s)
set query
set是⼀一个可迭代对象. 所以可以进⾏行行for循环
for el in s:
print(el)
set collection of other operations
s1 = {1,2,3,4,5,6,7}
s2 = {3,4,5,6}
# print(s1 - s2) #差集
# print(s1 | s2) #并集 (合集)
# print(s1 & s2) #交集
# print(s1 ^ s2) #对称差集 -- 反交集
# print(s1 > s2) # 超集 -- 父集
# print(s1 < s2) # 子集
to re-set collection
li = [1,2,3,4,5,2,2,2,33,3,3,2,2,1,]
print(list(set(li)))]
This itself is a collection set can be sent ⽣ chest upwards green change. is not the hash. We can use the stored data to use frozenset. frozenset is immutable. ⼀ is a type of data may be hashed
s = frozenset([1,2,3,"123",False,(1,2,3,4),"玲玲"])
dic = {s:'123'} # 可以正常使⽤用了了
print(dic)
6.3 copy depth
- Assignment:
Multiple variables pointing to the same memory address. For the list, set, dict, the direct assignment is to address to the memory variable, not a copy of the content.
l1 = [1,2,3,4,[5,6,7,]]
l2 = l1 #赋值l2赋值与l1不是拷贝两个变量指向一个列内存地址
l1.append(8)
print(l1) #[1, 2, 3, 4, [5, 6, 7], 8]
print(l2) #[1, 2, 3, 4, [5, 6, 7], 8]
Variable values l1 and l2 are the result of a point to the same memory address so the output is the same.
- Shallow copy:
Shallow copy, copy only the first layer, the second layer will not be copied, it is referred to as a shallow copy
l1 = [1,2,3,4,[4,5,6]]
l2 = l1[:] #表示浅拷贝
l2 = l1.copy() #表示浅拷贝
print(l1) #[1, 2, 3, 4, [5, 6, 7], 8]
print(l2) #[1, 2, 3, 4, [5, 6, 7], 8]
print(id(l1[0]))
print(id(l2[0]))
- Shallow copy creates a new list (containers), such as [3,4,5]
- L2 newly created list of elements in the list L1 and original elements used is the same memory space
li = [1,2,32,[3,4,5]]
l2 = li.copy()
li.append(8)
print(li) #[1, 2, 32, [3, 4, 5], 8]发生LI改变添加8元素
print(l2) #[1, 2, 32, [3, 4, 5]] L2不变
l1 = [1,2,3,(1,2,3)]
l2 = l1.copy()
l2[-1] = 6
print(l1) #[1, 2, 3, (1, 2, 3)]
print(l2) #[1, 2, 3, 6]
l1 = [1,2,3,4,[5,6,7,]]
l2 = l1.copy()
l1[-1].append(8) #L1向最后一位添加一个元素发生变化
print(l1) #[1, 2, 3, 4, [5, 6, 7, 8]]
print(l2) #[1, 2, 3, 4, [5, 6, 7, 8]] L2跟L1用的是同一个内存地址发生变化
l1 = [1,2,3,[4,5,6]]
l2 = l1.copy()
l2[-1] = 77
print(l1) # [1, 2, 3, [4, 5, 6]]
print(l2) # [1, 2, 3, 77]
- Deep copy:
- The original list of data types and immutable point to the same space, will create a new variable data space.
import copy
li = [1,2,3,4,5,[6,7,8]]
l2 = copy.deepcopy(li)
li[-1].append(678)
print(l2)
import copy
li = [3,4,5,[1,2,3,4],6,7,8,{"k":'v'}]
l2 = copy.deepcopy(li)
li[-1]['k'] = "a"
print(li)
print(l2)
dic = {} # "v":4
li = [] #[{"v":0},{"v":1},{"v":2},{"v":3},{"v":4}]
for i in range(0,5):
dic["v"] = i
li.append(dic)
print(li)