博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python全栈开发 * 07知识点汇总 * 180607
阅读量:6695 次
发布时间:2019-06-25

本文共 3823 字,大约阅读时间需要 12 分钟。

    07  set集合,深浅拷⻉以及部分知识点补充

一.while,for 循环知识点补充

二.int, str的相关操作

1.列表变字符串
# lst=["红","橙","黄","绿","青","蓝","紫"]
方法一 # s="_".join(lst)
# print(s)
方法二 # sum=""
# for i in lst: #     sum=sum+i+"_" # print(sum) #  字符串连接
# s="金","木","水","火","土" # s1="_".join(s) # print(s1)
元组连接 # tu=("男","女","老","少") # tu1="_".join(tu) # print(tu1)  2  list删除问题
# lst=["红","橙","黄","绿","青","蓝","紫"]
方法一   clear
# lst.clear() # print(lst)
方法二  del     range(0,len(lst))
# for i in range(0,7): #   del lst[0] # print(lst) 方法三    pop    range(0,len(lst))
# for i in range(0,7): #     lst.pop(0) # print(lst)
方法四     把要删除的元素保存在新列表中,循环新列表,删除旧列表.
# lst1=[] # for i in lst: #     lst1.append(i) # for el in lst1: #     lst.remove(el) # print(lst)
3  dict 中的fromkey( )
#fromkey :  第一个参数中每一个拿出来和第二个参数组织成键值对. #注意代码中只是更改了jay那个列表. 但是由于jay和JJ⽤的是同⼀个列表. 所以. 前⾯那个改了. 后⾯那个也会跟着改.
# dic=dict.fromkeys(["jay","jj"],["周杰伦","林俊杰"]) # dic.get("jay").append("蔡依林") # print(dic)
4. dic中的元素在迭代过程中是不可以删除的.,需要把要删除的内容存在新列表中,循环新列表,删除旧列表
# lst=["红","橙","黄","绿","青","蓝","紫"] # lst1=["黄","绿"] # for el in lst1: #     lst.remove(el) # print(lst)
5.  set() 集合 是dict类型的数据,只存key的值,不存value的值.
特点: 元素不重复(应用"去重");无序;内容必须不可变(可哈希);set本身是可变的(增删改).
去重:
# lst=["红","橙","黄","绿","青","蓝","紫","青","绿"] #  s=set(lst) #  print(s)
set 是可变的,里面内容是不可变的
# s={"alex",1,True,{"你好"}}    不可行,因为{"你好"}不可哈希( 可变的) #s={"麻花",[1,2,3,]}   不可行  因为[1,2,3]是列表,列表不可哈希( 可变的) #s={"听","说","读","写",(1,3)}   可行  因为(1,3)是元组,元组是不可变的(可哈希)
6.set  增  删  改  查
增一
# s=set() # s.add("大神") # print(s)
增二   update迭代更新
# s={"赵","钱","孙","李","张","王"} # s.update("吉孔孟") # print(s) # s.update(["中国"]) # print(s) # s.update(["中","国"]) # print(s)
删一  pop()  随机删除
# s={"赵","钱","孙","李","张","王"} # s1=s.pop() # print(s1)   删除的元素 # print(s)  删除以后的结果
# 删二clear   打印出来是set()
删三remove
# s.remove("王")   删除指定元素 # print(s)
#修改  set无法直接修改,先删掉,再增加.
# s={"赵","钱","孙","李","张","王"} # s.remove("王") # s.add("吉") # print(s)
查询
# s={"赵","钱","孙","李","张","王"} # for el in s: #     print(el)
7.   其他操作
#交集    print(s1 and s2)
# s1={"赵","钱","孙","李","张","王"} # s2={"赵","钱","孙","李","张","王","吉","宋"} # print(s1&s2) # print(s1.intersection(s2))
并集
# s1={"赵","钱","孙","李","张","王"} # s2={"赵","钱","孙","李","张","王","吉","宋"} # print(s1|s2) # print(s1.union(s2))
差集 print(s1-s2)  得到第一个中单独存在的
# s1={"赵","钱","孙","李","张","王","杜"} # s2={"赵","钱","孙","李","张","王","吉","宋"} # print(s1-s2) # print(s1.difference(s2))
反交集   两个集合中单独存在的数据  (和交集相反)
# s1={"赵","钱","孙","李","张","王","杜"} # s2={"赵","钱","孙","李","张","王","吉","宋"} # print(s1^s2) # print(s1.symmetric_difference(s2))
#子集  print(s1
# s1={"赵","钱","孙","李","张","王",} # s2={"赵","钱","孙","李","张","王","吉","宋"} # # print(s1
超集  print(s1
# s1={"赵","钱","孙","李","张","王",} # s2={"赵","钱","孙","李","张","王","吉","宋"} # print(s1>s2) # print(s1.issuperset(s2))
8.  set集合本身是可以发⽣改变的. 是不可hash的. 我们可以使⽤frozenset来保存数据.frozenset是不可变的. 也就是⼀个可哈希的数据类型
# s1={"赵","钱","孙","李","张","王",} # s=frozenset(s1) # dic={s1:"123"} # print(dic)
#9.深浅拷贝 (1)赋值:对于list, set, dict来说, 直接赋值. 其实是把内存地址交给变量. 并不是复制⼀份内容. 所以.lst1的内存指向和lst2是⼀样的. lst1改变了, lst2也发⽣了改变
list列表
# lst1=["红","橙","黄","绿","青","蓝","紫"] # lst2=lst1 # print(lst1) # print(lst2) # lst1.append("粉") # print(lst1) # print(lst2)
ict字典
# dic1={"颜色":"红色","水果":"香蕉"} # dic2=dic1 # print(dic1) # print(dic2) # dic1["蔬菜"]="番茄" # print(dic1) # print(dic2)
(2)浅拷贝    只拷贝第一层,第二层的内容不拷贝
# lst1=["风","雨","雪","霜"] # lst2=lst1.copy() # lst1.append("冰雹") # print(lst1) # print(lst2) # print(id(lst1),id(lst2))   2 # lst2=["风","雨","雪","霜",["电","雷"]] # lst3=lst2.copy() # print(lst3) # lst2[4].append("冰雹") # print(lst2) # print(lst3)
(3)深拷贝    把元素内部的元素完全进⾏拷⻉复制. 不会产⽣⼀个改变另⼀个跟着改变的问题
# import copy #  lst1=["番茄","白菜","胡萝卜",["苹果","荔枝","香蕉"]] #  lst2=copy.deepcopy(lst1) #  print(lst1) #  print(lst2) #  lst1[3].append("芒果") #  print(lst1) #  print(lst2)
 

.

 

 

 

 

 

 

 

 

 

 

list的删除问题

四.set 的集合

五深浅拷贝

转载于:https://www.cnblogs.com/J-7-H-2-F-7/p/9152421.html

你可能感兴趣的文章
Git基础入门(二)Git安装和配置
查看>>
mac sourcetree的远程创库验证名字出错修改
查看>>
翻译连载 | 附录 A:Transducing(上)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇...
查看>>
当今世界依然屏蔽谷歌服务的五个国家
查看>>
Go语言版冒泡排序
查看>>
LAMP--Apache 配置防盗链
查看>>
Linux系统开机启动过程
查看>>
PHP碎码——ci的验证码
查看>>
测试ldap后的报错
查看>>
-JavaEE- 如何将jsp页面生成word文档
查看>>
调教Windows Phone的Zune必知捷径
查看>>
Google Drive一触即发:云端同步存储
查看>>
Linux下java程序启动脚本(可做开机自启动Service脚本)
查看>>
linux安装mysql
查看>>
ContiPerf接口性能测试
查看>>
linux log查看(转载)
查看>>
dedecms手机站如何ajax提交数据到自定义表单
查看>>
jquery通过新浪财经的接口获取股票走势图和股票数据
查看>>
elasticsearch安装ik中文分词插件
查看>>
JSP中request.getParameter()乱码问题
查看>>