设为首页 收藏本站
查看: 1148|回复: 0

[经验分享] python中set集合常用方法

[复制链接]

尚未签到

发表于 2018-8-16 08:27:09 | 显示全部楼层 |阅读模式
  今天开始学习python的集合相关方法的使用:
  #集合的定义:集合是无序的,不重复的数据集合
set={"123","456","11"}  #add:向集合中填加元素
Add an element to a set.set.add(563)  
print(set)
  #Clear:清除集合中的元素
  Remove all elements from this set.
set.clear()  
print(set)返回空
  #Copy:复制一个集合并赋值给一个新的集合
  set3=set2.copy()
  #difference():打印set中和set2不一样的元素
set3=set.difference(set2)  
print(set3)
  #different_update():把集合set中和set2不一样的元素取出来并且更新到set里面
Remove all elements of another set from this set.set.difference_update(set2)  
print(set)
  #discard():如果一个元素属于该集合则删除,如果不属于该集合则什么也不做
Remove an element from a set if it is a member.  
If the element is not a member, do nothing.
  
set.discard("1234")
  
print(set)
  
Remove an element from a set; it must be a member.
  
If the element is not a member, raise a KeyError.
  
set.remove("123")
  
print(set)
  #pop():随机删除集合里面的元素,如果集合为空则报错,建议使用discard
Remove and return an arbitrary set element.  
Raises KeyError if the set is empty.
  
set.pop("11")
  
print(set)
  # Intersection:将set和set2两个集合的交集放入集合3并打印。
Return the intersection of two sets as a new set.set3=set.intersection(set2)  
print(set3)
  #intersection_update():将set和set2两个集合的交集更新到set中。
Update a set with the intersection of itself and another.set.intersection_update(set2)  
print(set)
  #isdisjoint():如果两个集合没有交集则返回true,反之如果有交集则返回false。
Return True if two sets have a null intersectionset3=.isdisjoint(set2)  
(set3)
  #issubset():set是否是set2的子集,如果是返回True,否则返回false
Report whether another set contains this set.set3=set.issubset(set2)  
print(set3)
  #issuperset():set是否是set2的父集,如果是则返回True,如果不是则返回False.
Report whether this set contains another set.set3=set.issuperset(set2)  
print(set3)
  #symmetric_difference():将两个集合中不是交集的部分取出来
Return the symmetric difference of two sets as a new set.set3=set.symmetric_difference(set2)  
print(set3)
  #symmetric_difference_update():取出set集合set2集合不是交集的部分,并且把值更新到set集合。
Update a set with the symmetric difference of itself and another..symmetric_difference_update(set2)  
()
  #union():取两个集合的合集,把所有元素汇集在一起形成新的集合,但是元素不重复
Return the union of sets as a new set=set.union(set2)  
(set)
  #update():把一个集合和另一个集合的合集更新到当前集合中,当前集合之前的数据被覆盖。
Update a set with the union of itself and others..update(set2)  
()
  上面就是所有关于集合的方法了,终于听完课以后自己把所有的方法都实验完了。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-552429-1-1.html 上篇帖子: Python学习记录day5-ygqygq2 下篇帖子: python中的logging-Tenderrain
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表