Python集合的常用操作
字典常用的就是,他的去重。 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集合的常用操作的更多相关文章
- 『无为则无心』Python序列 — 22、Python集合及其常用操作
目录 1.Python集合特点 2.Python集合的创建 3.操作集合常用API (1)增加数据 @1.add()方法 @2.update()方法 (2)删除数据 @1.remove()方法 @2. ...
- [PY3]——内置数据结构(6)——集合及其常用操作
集合及其常用操作Xmind图 集合的定义 # set( ) # {0,1,2} //注意不能用空的大括号来定义集合 # set(可迭代对象) In [1]: s=set();type ...
- Python集合类型的操作与应用
Python集合类型的操作与应用 一.Python集合类型 Python中的集合类型是一个包含0个或多个数据项的无序的.不重复的数据组合,其中,元素类型只能是固定数据类型,如整数.浮点数.字符串.元组 ...
- Redis集合的常用操作指令
Redis集合的常用操作指令 Sets常用操作指令 SADD 将指定的元素添加到集合.如果集合中存在该元素,则忽略. 如果集合不存在,会先创建一个集合然后在添加元素. 127.0.0.1:6379&g ...
- 二叉树的python可视化和常用操作代码
二叉树是一个重要的数据结构, 本文基于"二叉查找树"的python可视化 pybst 包, 做了一些改造, 可以支持更一般的"二叉树"可视化. 关于二叉树和二叉 ...
- Python数据类型及常用操作
Python字符串类型 1.用途: 用来记录有描述性的状态.比如:人名,地址等. 2.定义方式: 创建字符串非常简单,在‘ ’,“ ”,‘’‘ ’‘’内一填写一系列的字符例如:msg='hello' ...
- Python字符串的常用操作学习
>>> name = "I love my job!" >>> name.capitalize() #首字母大写 'I love my job! ...
- python os 模块常用操作
python 2.7 os 常用操作 官方document链接 文件和目录 os.access(path, mode) 读写权限测试 应用: try: fp = open("myfile&q ...
- 初识python: 字符串常用操作
直接上代码示例: #!/user/bin env python # author:Simple-Sir # time:20180914 # 字符串常用操作 name = 'lzh lyh' print ...
随机推荐
- 51nod 1225
题目 题解:看数据范围就估计是根号算法.考虑我们要求的式子: $ \sum\limits_{i = 1}^n {n - \left\lfloor {\frac{n}{i}} \right\rfloor ...
- Codeforces 148D Bag of mice:概率dp 记忆化搜索
题目链接:http://codeforces.com/problemset/problem/148/D 题意: 一个袋子中有w只白老鼠,b只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...
- 分享知识-快乐自己:oracle12c创建用户提示ORA-65096:公用用户名或角色无效
今天在oracle12c上创建用户,报错了.如下图: 我很郁闷, 就打开了oracle官方网站找了下, 发现创建用户是有限制的. 2.解决方案 创建用户的时候用户名以c##或者C##开头即可. 错误写 ...
- L88
Where You Vote May Affect How You Vote On election day, where do you vote? If it's in a church, you ...
- storm源码剖析(2):storm的配置项
storm的配置项,可以从backtype/storm/Config.java中找到所有配置项及其描述
- 模拟jQuery的一些功能
//getStyle function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; } else{ ...
- [Selenium] 处理表格(python + java)
python : https://www.cnblogs.com/yan-xiang/p/6819168.html 操作内容:获取table总行数.总列数.获取某单元格的text值,删除一行[如果每行 ...
- BZOJ_4025_二分图_线段树按时间分治+并查集
BZOJ_4025_二分图_线段树按时间分治+并查集 Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简 ...
- 「JLOI2011」「LuoguP4568」飞行路线(分层图最短路
题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在nn个城市设有业务,设这些城市分别标记为00到n-1n−1,一共有mm种航线,每种航线连接两个城市,并且 ...
- 「 JSOI2004」「LuoguP1337」平衡点 / 吊打XXX(模拟退火
题目描述 如图:有n个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.图中X处就是公共的绳结.假设绳子是完全弹性的(不会造成能量损失),桌子足够高(因而重物不会垂到 ...