今日内容:
1、深浅拷贝
2、元组
3、字典
4、set
1、深浅拷贝
# 1、值拷贝 # 采用赋值的方法进行 # 只会将堆区容器变量与栈区的绑定关系进行复制 # 2、浅拷贝 # 会将堆区与栈区的绑定关系,以及容器类型中包含的每个元素的id进行复制 # 使用方法为 容器类型.copy() # 3、深拷贝 # 会将绑定关系、容器类型、每个元素的值重新复制然后开辟空间进行存储 # 使用方法为: from copy import deepcopy deepcopy(原容器)
2、元组
""" 元组的基本操作及常用方法 """ """ 基本操作 """ # 1、定义 t1 = (1,5,8,7,6,[10,20,40,50,30],"a","b","c") # 2、索引取值 print(t1[5]) # [10, 20, 40, 50, 30] # 3、切片 print(t1[6::1]) # ('a', 'b', 'c') # 4、遍历 for i in t1: print(i,end=' ') # 1 5 8 7 6 [10, 20, 40, 50, 30] a b c print() # 5、计算长度 print(len(t1)) # 6、拼接 print(t1 + (100,200)) # (1, 5, 8, 7, 6, [10, 20, 40, 50, 30], 'a', 'b', 'c', 100, 200) # 7、成员运算 print(100 in t1) # False """ 方法 """ # 1、计数 print(t1.count(1,)) # 1 # 2、查询索引 print(t1.index(8)) # 2
2、字典的操作及常用方法
(1)常用操作
""" 字典的操作 """ # 1、字典的定义 dic = {} dic = dict(name="andy",age=18,gender="man") # 括号必须用小括号 dic = {}.fromkeys([0,1,2,3,4]) # 将第一个参数作为key值,如果设置第二个参数,则value值为设置的值,如果不设置第二个参数,则默认为 None
# 使用fromkeys创建字典时,所有的值都指向一个空间,如果该空间为可变类型,那么通过key改变一个值,其余的值也会随之改变。
print(dic) dic = {"name":"andy","age":18,"gender":"man"} # 1、根据key取值 print(dic["name"]) # andy # 2、计算长度 print(len(dic)) # 3 # 3、成员运算 ---> 进行key值得成员运算 print("name" in dic) # True print("andy" in dic) # False # 4、遍历 for i in dic: # ----> 取出的是key值 print(i) for k,y in dic.items(): print(k,y)
(2)常用方法
""" 常用方法: """ # 1、增 --> 如果字典中不存在某个key,只需要进行赋值操作就可以 dic["weight"] = "50" print(dic) # {'name': 'andy', 'age': 18, 'gender': 'man', 'weight': '50'} # 2、改 --> 如果字典中存在某个key,只需要进行赋值操作就可以 dic["name"] = "lice" print(dic) # {'name': 'lice', 'age': 18, 'gender': 'man', 'weight': '50'} # 3、查 --> 可以通过key值进行查值 print(dic["age"]) # 18 --> 如果没有索引的key值,会报错 print(dic.get("haha","没有")) # 没有 ---> 没有索引的key值,会返回给定的结果 # 4、删 pop / popitem item = dic.pop("name") # --> 存在一个返回值,这个返回值就是value print(item) # ---> 返回值是value print(dic) # {'age': 18, 'gender': 'man', 'weight': '50'} item = dic.popitem() # 取出最后一个值 print(item) # dic.clear() # 清空字符串 # 从字典里面进行取值 print(dic.items()) # 取出所有的键值对,并以迭代器进行存储 ---> dict_items([('age', 18), ('gender', 'man')]) print(dic.values()) # 取出值,以迭代器尽心存储 ---> dict_values([18, 'man']) print(dic.keys()) # 取出key值,以迭代器进行存储 ---> dict_keys(['age', 'gender'])
(3)其它方法
""" 其它方法 """ # 1、update 方法: dic = {"name":"andy","age":18,"gender":"man"} dic_new = {"name":"li","weight":50,"address":"河北"} dic.update(dic_new) print(dic) # {'name': 'li', 'age': 18, 'gender': 'man', 'weight': 50, 'address': '河北'} dic_new.update(dic) print(dic_new) # {'name': 'li', 'weight': 50, 'address': '河北', 'age': 18, 'gender': 'man'} # 2、setdefault 方法: dic = {"name":"andy","age":18,"gender":"man"} dic_new = {"name":"li","weight":50,"address":"河北"} dic.setdefault("key",10) # ---> setdefault 括号中至少有一个值,是key,括号中两个值一个是key,一个是value print(dic) # {'name': 'andy', 'age': 18, 'gender': 'man', 'key': 10}
3、set类型
(1)常用操作
""" 常用操作 """ # 1、定义 s1 = set() # ---> 空集合的定义 # 2、增加 s1.add(1) # ---> 将括号中的值添加进 s1 # 3、删除 s1.remove(1) # ---> 从集合中删除指定值 s1 = {"2",'3','5','4','9','6','7','8',"1"} item = s1.pop() # ---> 删除一个值,并将值进行返回 # print(item) s1.clear() # ---> 删除集合中的所有值
(2)运算
""" 集合的运算 """ # 1、交集 & ---> 两个集合都包含的值 se1 = {1,2,5,9,8} se2 = {1,10,7,9,6} items = se1 & se2 print(items) # --> 是一个集合类型 {1, 9} # 2、合集 | ---> 两个的总和 print(se1 | se2) # {1, 2, 5, 6, 7, 8, 9, 10} # 3、对称交集 ^ ----> 总和 - 交集 print(se1 ^ se2) # {2, 5, 6, 7, 8, 10} # 4、差集 - 集合中独有的部分 print(se1 - se2) # se1中有而se2中没有的 {8, 2, 5} print(se2 - se1) # se2中有而se1中没有的 {10, 6, 7} # 5、比较 < 必须是包含与被包含关系 s5 = {1,2} s6 = {1,2,3} print(s5 < s6) # True print(s5 > s6) # False