标签:add value rom 获取 基础数据类型 enc 介绍 nio 保存
li = ["李嘉诚", "麻花藤", "?海峰", "刘嘉玲"] s = "_".join(li) print(s)
li = "?花?闺?" s = "_".join(li) print(s)
li = [11, 22, 33, 44] for e in li: li.remove(e) print(li)
结果: [22, 44]
li = [11, 22, 33, 44] for i in range(0, len(li)): del li[i] print(li)
结果: 报错 # i= 0, 1, 2 删除的时候li[0] 被删除之后. 后??个就变成了第0个. # 以此类推. 当i = 2的时候. list中只有?个元素. 但是这个时候删除的是第2个 肯定报错啊
for el in li: li.pop() # pop也不? print(li)
结果: [11, 22]
for i in range(0, len(li)): # 循环len(li)次, 然后从后往前删除 li.pop() print(li)
li = [11, 22, 33, 44] del_li = [] for e in li: del_li.append(e) for e in del_li: li.remove(e) print(li)
dic = dict.fromkeys(["jay", "JJ"], ["周杰伦", "麻花藤"]) print(dic)
结果: {‘jay‘: [‘周杰伦‘, ‘麻花藤‘], ‘JJ‘: [‘周杰伦‘, ‘麻花藤‘]}
dic = dict.fromkeys(["jay", "JJ"], ["周杰伦", "麻花藤"]) print(dic)
dic.get("jay").append("胡?") print(dic)
结果: {‘jay‘: [‘周杰伦‘, ‘麻花藤‘, ‘胡?‘], ‘JJ‘: [‘周杰伦‘, ‘麻花藤‘, ‘胡?‘]}
dic = {‘k1‘: ‘alex‘, ‘k2‘: ‘wusir‘, ‘s1‘: ‘??板‘} # 删除key中带有‘k‘的元素 for k in dic: if ‘k‘ in k: del dic[k] # dictionary changed size during iteration, 在循环迭代的时候不允许进?删除操作 print(dic)
dic = {‘k1‘: ‘alex‘, ‘k2‘: ‘wusir‘, ‘s1‘: ‘??板‘} dic_del_list = [] # 删除key中带有‘k‘的元素 for k in dic: if ‘k‘ in k: dic_del_list.append(k)
for el in dic_del_list: del dic[el] print(dic)
set1 = {‘1‘,‘alex‘,2,True,[1,2,3]} # 报错 set2 = {‘1‘,‘alex‘,2,True,{1:2}} # 报错 set3 = {‘1‘,‘alex‘,2,True,(1,2,[2,3,4])} # 报错
s = {"周杰伦", "周杰伦", "周星星"} print(s) 结果: {‘周星星‘, ‘周杰伦‘}
# 给list去重复 lst = [45, 5, "哈哈", 45, ‘哈哈‘, 50] lst = list(set(lst)) # 把list转换成set, 然后再转换回list print(lst)
s = {"刘嘉玲", ‘关之琳‘, "王祖贤"} s.add("郑裕玲") print(s)
s.add("郑裕玲") # 重复的内容不会被添加到set集合中 print(s)
s = {"刘嘉玲", ‘关之琳‘, "王祖贤"} s.update("麻花藤") # 迭代更新 print(s)
s.update(["张曼?", "李若彤","李若彤"]) print(s)
s = {"刘嘉玲", ‘关之琳‘, "王祖贤","张曼?", "李若彤"} item = s.pop() # 随机弹出?个. print(s) print(item)
s.remove("关之琳") # 直接删除元素 # s.remove("??疼") # 不存在这个元素. 删除会报错 print(s)
s.clear() # 清空set集合.需要注意的是set集合如果是空的. 打印出来是set() 因为要和 dict区分的. print(s) # set()
# set集合中的数据没有索引. 也没有办法去定位?个元素. 所以没有办法进?直接修改. # 我们可以采?先删除后添加的?式来完成修改操作 s = {"刘嘉玲", ‘关之琳‘, "王祖贤","张曼?", "李若彤"} # 把刘嘉玲改成赵本? s.remove("刘嘉玲") s.add("赵本?") print(s)
# set是?个可迭代对象. 所以可以进?for循环 for el in s: print(el)
s1 = {"刘能", "赵四", "???"} s2 = {"刘科?", "冯乡?", "???"}
# 交集 # 两个集合中的共有元素 print(s1 & s2) # {‘???‘} print(s1.intersection(s2)) # {‘???‘}
# 并集 print(s1 | s2) # {‘刘科?‘, ‘冯乡?‘, ‘赵四‘, ‘???‘, ‘刘能‘} print(s1.union(s2)) # {‘刘科?‘, ‘冯乡?‘, ‘赵四‘, ‘???‘, ‘刘能‘}
# 差集 print(s1 - s2) # {‘赵四‘, ‘刘能‘} 得到第?个中单独存在的 print(s1.difference(s2)) # {‘赵四‘, ‘刘能‘}
# 反交集 print(s1 ^ s2) # 两个集合中单独存在的数据 {‘冯乡?‘, ‘刘能‘, ‘刘科?‘, ‘赵四‘} print(s1.symmetric_difference(s2)) # {‘冯乡?‘, ‘刘能‘, ‘刘科?‘, ‘赵四‘}
s1 = {"刘能", "赵四"} s2 = {"刘能", "赵四", "???"}
# ?集 print(s1 < s2) # set1是set2的?集吗? True print(s1.issubset(s2))
# 超集 print(s1 > s2) # set1是set2的超集吗? False print(s1.issuperset(s2))
s = frozenset(["赵本?", "刘能", "???", "?跪"])
dic = {s:‘123‘} # 可以正常使?了 print(dic)
lst1 = ["??狮王", "紫衫?王", "?眉鹰王", "?翼蝠王"] lst2 = lst1
print(lst1) print(lst2)
lst1.append("杨逍") print(lst1) print(lst2)
结果: [‘??狮王‘, ‘紫衫?王‘, ‘?眉鹰王‘, ‘?翼蝠王‘, ‘杨逍‘] [‘??狮王‘, ‘紫衫?王‘, ‘?眉鹰王‘, ‘?翼蝠王‘, ‘杨逍‘]
dic1 = {"id": 123, "name": "谢逊"} dic2 = dic1 print(dic1) print(dic2)
dic1[‘name‘] = "范瑶" print(dic1) print(dic2)
结果: {‘id‘: 123, ‘name‘: ‘谢逊‘} {‘id‘: 123, ‘name‘: ‘谢逊‘} {‘id‘: 123, ‘name‘: ‘范瑶‘} {‘id‘: 123, ‘name‘: ‘范瑶‘}
lst1 = ["何炅", "杜海涛","周渝?"] lst2 = lst1.copy() lst1.append("李嘉诚") print(lst1) print(lst2) print(id(lst1), id(lst2))
结果: 两个lst完全不?样. 内存地址和内容也不?样. 发现实现了内存的拷?
lst1 = ["何炅", "杜海涛","周渝?", ["麻花藤", "?芸", "周笔畅"]] lst2 = lst1.copy() lst1[3].append("?敌是多磨寂寞") print(lst1) print(lst2) print(id(lst1[3]), id(lst2[3]))
结果: [‘何炅‘, ‘杜海涛‘, ‘周渝?‘, [‘麻花藤‘, ‘?芸‘, ‘周笔畅‘, ‘?敌是多磨寂寞‘]] [‘何炅‘, ‘杜海涛‘, ‘周渝?‘, [‘麻花藤‘, ‘?芸‘, ‘周笔畅‘, ‘?敌是多磨寂寞‘]] 4417248328 4417248328
import copy lst1 = ["何炅", "杜海涛","周渝?", ["麻花藤", "?芸", "周笔畅"]] lst2 = copy.deepcopy(lst1) lst1[3].append("?敌是多磨寂寞") print(lst1) print(lst2) print(id(lst1[3]), id(lst2[3]))
结果: [‘何炅‘, ‘杜海涛‘, ‘周渝?‘, [‘麻花藤‘, ‘?芸‘, ‘周笔畅‘, ‘?敌是多磨寂寞‘]] [‘何炅‘, ‘杜海涛‘, ‘周渝?‘, [‘麻花藤‘, ‘?芸‘, ‘周笔畅‘]] 4447221448 4447233800
python基础(9):基本数据类型四(set集合)、基础数据类型补充、深浅拷贝
标签:add value rom 获取 基础数据类型 enc 介绍 nio 保存
原文地址:https://www.cnblogs.com/liuhui0308/p/11809031.html