字典常用的就是,他的去重。

set集合是python的一个基本数据类型.
set中的元素是不重复的.⽆无序的.⾥面的元素必须是可hash的(int, str, tuple,bool)。
我们可以这样来记. set就是dict类型的数据但是不保存value, 只保存key. set也⽤{}表⽰
注意:
set中的元素是不重复的, 且无序的.
使⽤用这个特性.我们可以使⽤用set来去掉重复
set集合中的元素必须是可hash的, 但是set本身是不可hash得. set是可变的。 set集合增删改查 1.增加
def add(self, *args, **kwargs): # real signature unknown
"""
Add an element to a set. This has no effect if the element is already present.
"""
pass 用法:添加一个元素到集合。重复的内容不会被添加到set集合。 例子:
s = {"刘大哥", '关大哥', "张大哥"}
s.add("赵子龙")
print(s)
s.add("赵子龙") # 重复的内容不不会被添加到set集合中
print(s) def update(self, *args, **kwargs): # real signature unknown
""" Update a set with the union of itself and others. """
pass 用法:迭代更新 例子:
s = {"刘大哥", '关大哥', "张大哥"}
s.update("赵子龙") # 迭代更更新
print(s)
s.update(["阿斗", "卧龙","凤雏"])
print(s) 2.删除
def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
"""
pass 用法:随机删除一个元素,如果集合为空,会报错。 例子: s = {"刘大哥", '关大哥', "王大哥","张哥哥"}
item = s.pop() # 随机弹出⼀一个.
print(s)
print(item) def remove(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.
"""
pass 用法:删除集合中指定成员,如果成员不存在会报错。 例子:
s = {"刘大哥", '关大哥', "王大哥","张哥哥"}
s.remove("关大哥") # 直接删除元素
# s.remove("⻢马") # 不不存在这个元素. 删除会报错
print(s) def discard(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set if it is a member. If the element is not a member, do nothing.
"""
pass 用法:删除集合中指定成员,如果该集合不存在所删除成员,不做任何操作,也不会报错。 例子:
s = {1, 3, 4, 5}
print(s)#{1, 3, 4, 5}
s.discard(3)
print(s)#{1, 4, 5}
s.discard(66)
print(s)#{1, 4, 5} def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from this set. """
pass clear() 清空set集合。
注意:set集合如果是空的。打印出来的是set()。需要和dict区分。
例子:
s = {1, 3, 4, 5}
s.clear()
print(s)。#set() 3.修改
set 集合中的数据没有索引,也没有办法定位一个元素。所以没有办法直接修改。
我们可以采用:
1)先删除后添加的方式
2)将set转为list删除元素后将list转为set 4.查询
set是一个可迭代对象,所以可以进行for循环
for x in s:
print(x) 5.常用操作 1.求差集(-)
def difference(self, *args, **kwargs): # real signature unknown
"""
Return the difference of two or more sets as a new set. (i.e. all elements that are in this set but not the others.)
"""
pass 例子:
s1 = {1, 3, 5, 7, 9}
s2 = {1, 3, 6, 8, 10}
print(s1-s2)#{9, 5, 7}
print(s1.difference(s2))#{9, 5, 7}
print(s2-s1)#{8, 10, 6}
print(s2.difference(s1))#{8, 10, 6} 2.求交集(&)
def intersection(self, *args, **kwargs): # real signature unknown
"""
Return the intersection of two sets as a new set. (i.e. all elements that are in both sets.)
"""
pass 例子:
s1 = {1, 3, 5, 7, 9}
s2 = {1, 3, 6, 8, 10}
print(s1&s2)#{1, 3}
print(s1.intersection(s2))
print(s2&s1)#{1, 3}
print(s2.intersection(s1)) 3.求对称差集(^)
def symmetric_difference(self, *args, **kwargs): # real signature unknown
"""
Return the symmetric difference of two sets as a new set. (i.e. all elements that are in exactly one of the sets.)
"""
pass 例子:
s1 = {1, 3, 5, 7, 9}
s2 = {1, 3, 6, 8, 10}
print(s1 ^ s2)#{5, 6, 7, 8, 9, 10}
print(s1.symmetric_difference(s2))#{5, 6, 7, 8, 9, 10}
print(s2 ^ s1)#{5, 6, 7, 8, 9, 10}
print(s2.symmetric_difference(s1))#{5, 6, 7, 8, 9, 10} 4.求并集(|)
def union(self, *args, **kwargs): # real signature unknown
"""
Return the union of sets as a new set. (i.e. all elements that are in either set.)
"""
pass 例子:
s1 = {1, 3, 5, 7, 9}
s2 = {1, 3, 6, 8, 10}
print(s1 | s2)#{1, 3, 5, 6, 7, 8, 9, 10}
print(s1.union(s2))#{1, 3, 5, 6, 7, 8, 9, 10}
print(s2 | s1)#{1, 3, 5, 6, 7, 8, 9, 10}
print(s2.union(s1))#{1, 3, 5, 6, 7, 8, 9, 10} 5.子集
def issubset(self, *args, **kwargs): # real signature unknown
""" Report whether another set contains this set. """
pass 例子:
s1 = {"1", "2"}
s2 = {"1", "2", "3"}
# ⼦子集
print(s1 < s2) # set1是set2的⼦子集吗? True
print(s1.issubset(s2)) 6.超集
def issuperset(self, *args, **kwargs): # real signature unknown
""" Report whether this set contains another set. """
pass 例子:
s1 = {"1", "2"}
s2 = {"1", "2", "3"}
print(s1 > s2) # set1是set2的超集吗? False
print(s1.issuperset(s2))

Python集合的常用操作的更多相关文章

  1. 『无为则无心』Python序列 — 22、Python集合及其常用操作

    目录 1.Python集合特点 2.Python集合的创建 3.操作集合常用API (1)增加数据 @1.add()方法 @2.update()方法 (2)删除数据 @1.remove()方法 @2. ...

  2. [PY3]——内置数据结构(6)——集合及其常用操作

    集合及其常用操作Xmind图          集合的定义 # set( ) # {0,1,2} //注意不能用空的大括号来定义集合 # set(可迭代对象) In [1]: s=set();type ...

  3. Python集合类型的操作与应用

    Python集合类型的操作与应用 一.Python集合类型 Python中的集合类型是一个包含0个或多个数据项的无序的.不重复的数据组合,其中,元素类型只能是固定数据类型,如整数.浮点数.字符串.元组 ...

  4. Redis集合的常用操作指令

    Redis集合的常用操作指令 Sets常用操作指令 SADD 将指定的元素添加到集合.如果集合中存在该元素,则忽略. 如果集合不存在,会先创建一个集合然后在添加元素. 127.0.0.1:6379&g ...

  5. 二叉树的python可视化和常用操作代码

    二叉树是一个重要的数据结构, 本文基于"二叉查找树"的python可视化 pybst 包, 做了一些改造, 可以支持更一般的"二叉树"可视化. 关于二叉树和二叉 ...

  6. Python数据类型及常用操作

    Python字符串类型 1.用途: 用来记录有描述性的状态.比如:人名,地址等. 2.定义方式: 创建字符串非常简单,在‘ ’,“ ”,‘’‘ ’‘’内一填写一系列的字符例如:msg='hello' ...

  7. Python字符串的常用操作学习

    >>> name = "I love my job!" >>> name.capitalize() #首字母大写 'I love my job! ...

  8. python os 模块常用操作

    python 2.7 os 常用操作 官方document链接 文件和目录 os.access(path, mode) 读写权限测试 应用: try: fp = open("myfile&q ...

  9. 初识python: 字符串常用操作

    直接上代码示例: #!/user/bin env python # author:Simple-Sir # time:20180914 # 字符串常用操作 name = 'lzh lyh' print ...

随机推荐

  1. BZOJ 1726 [Usaco2006 Nov]Roadblocks第二短路:双向spfa【次短路】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1726 题意: 给你一个无向图,求次短路. 题解: 两种方法. 方法一: 一遍spfa,在s ...

  2. 分享知识-快乐自己:微服务的注册与发现(基于Eureka)

    1):微服务架构 服务提供者.服务消费者.服务发现组件这三者之间的关系: 各个微服务在启动时,将自己的网络地址等信息注册到服务发现组件中,服务发现组件会存储这些信息. 服务消费者可从服务发现组件查询服 ...

  3. 001-Bootstrap栅格系统

    1 安装和基本使用 外文官网 中文官网 可以正常下载使用 有三个文件夹, 分别是css, fonts, js bootstrap/ ├── css/ │ ├── bootstrap.css │ ├── ...

  4. linux 进程学习笔记-named pipe (FIFO)命名管道

    与“无名管道”不同的是,FIFO拥有一个名称来标志它,所谓的名称实际上就是一个路径,比如“/tmp/my_fifo”,其对应到磁盘上的一个管道文件,如果我们用file命令来查看其文件类型的话,会得到如 ...

  5. Map功能简化Python并发代码

    <转摘>Python 并行任务技巧 支持Map并发的包文件有两个: Multiprocessing,还有少为人知的但却功能强大的子文件 multiprocessing.dummy. Dum ...

  6. MySQL活动期间订单满600元并且在活动日期之前超过30天没有下过单_20161030

    计算 活动期间订单满600元并且在活动日期之前超过30天没有下过单 首先拿到这个需求,首先需要明确活动日期区间 10.29-10.31,其次要取这个时间段内某天订单额最高的那天及订单额,再次需要判断这 ...

  7. ACM学习历程—SNNUOJ 1110 传输网络((并查集 && 离线) || (线段树 && 时间戳))(2015陕西省大学生程序设计竞赛D题)

    Description Byteland国家的网络单向传输系统可以被看成是以首都 Bytetown为中心的有向树,一开始只有Bytetown建有基站,所有其他城市的信号都是从Bytetown传输过来的 ...

  8. cocos2dx unzip、createDir

    转自:http://www.cnblogs.com/xioapingguo/p/4037323.html static unsigned long _maxUnzipBufSize = 0x50000 ...

  9. 如何在VS2015中使用Git命令提示符

    本文转载自 http://qkxue.net/info/176223/Visual-Studio-Git VS2015自带了Git插件,但有时候觉得Git控制台命令更方便些.VS中本身不能把Git B ...

  10. 1. md5 collision(50)

    md5 collision(50)      ------南京邮电大学ctf: http://chinalover.sinaapp.com/web19/ 发现了一串代码 <?php $md51 ...