python入门,数据容器:set集合
发布时间:2024年01月14日
set最大的特点就是不支持重复元素,可以进行元素的去重处理,但不有序,不保证元素顺序正确
所以就不能使用下标索引的访问
1.集合的定义
集合的定义使用的是大括号{ }

?对ok这个字符串进行了去重
2.add添加新元素

?
3.remove移除元素

?4.pop随机取元素

?注意:
取出那个元素后,set中就没有那个元素了
5.clear清空集合


6.difference取差集
取出集合1和集合2的差集,就是说集合1有而集合2没有的会被取出来?

?
?7.difference_update消除两个集合的差集
在集合1内部删除和集合2相同的元素
集合1被修改,集合2不变

?8.union合并元素
将集合1和集合2合并为一个新的集合3

?
9.len统计集合元素数量

?
10.集合的遍历
集合不支持下标索引,所以不能使用while循环,要用for循环

?
文章来源:https://blog.csdn.net/2301_79431343/article/details/135568250
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:chenni525@qq.com进行投诉反馈,一经查实,立即删除!