1.dict和list中copy方法画图加代码解释
List浅拷贝:b = a: 赋值引用,a 和 b 都指向同一个对象;
b = a.copy(): a 和 b 是一个独立的对象,但他们的子对象还是指向统一对象(是引用)。
dict_fjq = {1: [1, 2, 3]}
dict_fjq2 = dict_fjq.copy()
print(dict_fjq)
dict_fjq[1].append(4)
print(dict_fjq2)
D:\python\python.exe D:/python/python_code/fjq_var.py
{1: [1, 2, 3]}
{1: [1, 2, 3, 4]}
Process finished with exit code 0
List深拷贝:b = copy.deepcopy(a): a 和 b 完全拷贝了父对象及其子对象,两者是完全独立的。
import copy
dict_fjq = {1: [1, 2, 3]}
dict_fjq2 = dict_fjq.copy()
print(dict_fjq, dict_fjq2)
dict_fjq2 = copy.deepcopy(dict_fjq)
dict_fjq[1].append(4)
print(dict_fjq, dict_fjq2)
D:\python\python.exe D:/python/python_code/fjq_var.py
{1: [1, 2, 3]} {1: [1, 2, 3]}
{1: [1, 2, 3, 4]} {1: [1, 2, 3]}
Process finished with exit code 0
dict浅拷贝:dict_data1和dict_data2两者是独立的,但是里面的元素指向同一个,所以改变一个后,另一个也会跟着变。
dict_data = {1: [1, 2, 3]}
dict_data2 = dict_data.copy()
print(dict_data2)
print(id(dict_data[1]))
print(id(dict_data2[1]))
print(id(dict_data))
print(id(dict_data2))
dict_data[1][1] = 8
print(dict_data)
print(dict_data2)
D:\python\python.exe D:/python/python_code/fjq_var.py
{1: [1, 2, 3]}
2070674903936
2070674903936
2070669058496
2070669058688
{1: [1, 8, 3]}
{1: [1, 8, 3]}
dict深拷贝:dict深拷贝完全拷贝,两者完全独立,即使改变一个,另一个仍保持不变
import copy
dict_data = {1: [1, 2, 3]}
dict_data2 = copy.deepcopy(dict_data)
print(dict_data2)
print(id(dict_data[1]))
print(id(dict_data2[1]))
print(id(dict_data))
print(id(dict_data2))
dict_data[1][1] = 8
print(dict_data)
print(dict_data2)
D:\python\python.exe D:/python/python_code/fjq_var.py
{1: [1, 2, 3]}
2024581536640
2024581534144
2024580014528
2024580069440
{1: [1, 8, 3]}
{1: [1, 2, 3]}
Process finished with exit code 0
2.dict中扩展字典长度(多种方法)
(1) setdefault-Insert key with a value of default if key is not in the dictionary.
如果键不在字典中,则插入值为默认值的键。
dict_data = {1: 2, 2: 3, 3: 4}
dict_data.setdefault(4)
print(dict_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
{1: 2, 2: 3, 3: 4, 4: None}
Process finished with exit code 0
(2)update-D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
从字典/可迭代 E 和 F 更新 D。
dict_data = {1: 2, 2: 3, 3: 4}
dict_data1 = {4: 5}
dict_data.update(dict_data1)
print(dict_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
{1: 2, 2: 3, 3: 4, 4: 5}
Process finished with exit code 0
(3)[key] = value
dict_data = {1: 2, 2: 3,}
dict_data[3] = 4
print(dict_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
{1: 2, 2: 3, 3: 4}
Process finished with exit code 0
3.dict中获取所有的key,获取所有的value,获取所有的item.
(1)keys
dict_fjq = {1: 7, 2: 8, 3: 9}
print(dict_fjq.keys())
D:\python\python.exe D:/python/python_code/fjq_var.py
dict_keys([1, 2, 3])
Process finished with exit code 0
(2)value
dict_fjq = {1: 7, 2: 8, 3: 9}
print(dict_fjq.values())
D:\python\python.exe D:/python/python_code/fjq_var.py
dict_values([7, 8, 9])
Process finished with exit code 0
(3)items
dict_fjq = {1: 7, 2: 8, 3: 9}
print(dict_fjq.items())
D:\python\python.exe D:/python/python_code/fjq_var.py
dict_items([(1, 7), (2, 8), (3, 9)])
Process finished with exit code 0
4.dict中通过一个不存在的key去获取值(要求不报错)
(1)get
dict_data = {1: 2, 2: 3, 3: 4}
print(dict_data.get(4))
D:\python\python.exe D:/python/python_code/fjq_var.py
None
Process finished with exit code 0
(2)setdefault
dict_data = {1: 2, 2: 3, 3: 4}
print(dict_data.setdefault(4))
D:\python\python.exe D:/python/python_code/fjq_var.py
None
Process finished with exit code 0
5.定义空集合
data = set()
print(data)
print(type(data))
D:\python\python.exe D:/python/python_code/fjq_var.py
set()
<class 'set'>
Process finished with exit code 0
6.set中,求差集,求合集,求交集(要求在原集合上操作)
添加一个元素
删除一个元素(使用多种方法)
(1) 差集:symmetric_difference_update
set_data = {1, 3, 5, 6, }
set_data1 = {1, 2, 3, 5, 6}
set_data.symmetric_difference_update(set_data1)
print(set_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
{2}
Process finished with exit code 0
(2)合集 : update
set_data = {1, 3, 5, 6, 7}
set_data1 = {1, 2, 3, 5, 6}
set_data.update(set_data1)
print(set_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
{1, 2, 3, 5, 6, 7}
Process finished with exit code 0
(3) 交集:intersection_update
set_data = {1, 3, 5}
set_data1 = {1, 2, 3, 4}
set_data.intersection_update(set_data1)
print(set_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
{1, 3}
Process finished with exit code 0
添加元素
add
set_data = {1, 3}
set_data.add(2)
print(set_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
{1, 2, 3}
Process finished with exit code 0
删除一个元素(多种方法)
pop
set_data = {1, 3, 4, 5, 6, 7}
data = set_data.pop()
print(data, set_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
1 {3, 4, 5, 6, 7}
Process finished with exit code 0
remove
set_data = {1, 3, 4, 5, 7, 8}
set_data.remove(1)
print(set_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
{3, 4, 5, 7, 8}
Process finished with exit code 0
discard
set_data = {1, 3, 5}
set_data.discard(1)
print(set_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
{3, 5}
Process finished with exit code 0
clear
set_data = {1, 3}
set_data.clear()
print(set_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
set()
Process finished with exit code 0
difference_update
set_data = {1, 3, 5}
set_data1 = {1}
set_data.difference_update(set_data1)
print(set_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
{3, 5}
Process finished with exit code 0