Python set 集合
简介
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联 合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.
创建集合
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
集合常用操作及实例展示
可以使用dir(set)查看集合支持的操作方法
add
功能:增加一个元素到集合。当集合存在该元素时,该语句不生效
Add an element to a set.This has no effect if the element is already present.
语法:S.add(object)
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S1.add('a')
>>> print S1
set(['a', 'i', 'p', 's', 'r', 't'])
>>> S1.add('a')
>>> print S1
set(['a', 'i', 'p', 's', 'r', 't'])
clear
功能:清空集合
Remove all elements from this set.
语法:S.clear()
实例展示:
>>>S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S1.clear()
>>> print S1
set([])
copy
功能:浅复制集合,返回一个新的集合。
Return a shallow copy of a set.
语法:S.copy()
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S2 = S1.copy()
>>> print S2
set(['i', 'p', 's', 'r', 't'])
>>> id(S2)
140239642434120
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> id(S1)
140239644667136
difference
功能:找出两个或多个集合中的不同元素,结果返回一个新的集合
Return the difference of two or more sets as a new set.(all elements that are in this set but not the others.)
语法:S.differencce(set1,set2.....)
实例展示:
>>> S1 = set('spirita')
>>> print S1
set(['a', 'i', 'p', 's', 'r', 't'])
>>> S2 = set('spiri')
>>> print S2
set(['i', 'p', 's', 'r'])
>>> S3 = set('liush')
>>> print S3
set(['i', 'h', 's', 'u', 'l'])
##################################################
#找出S1和S2中的不同元素,结果返回一个新的集合
>>> S1.difference(S2)
set(['a', 't'])
##################################################
#找出S1和S3中的不同元素,结果返回一个新的集合
>>> S1.difference(S3)
set(['a', 'p', 'r', 't'])
##################################################
#找出S1、S2和S3中的不同元素,结果返回一个新的集合
>>> S1.difference(S2,S3)
set(['a', 't'])
difference_update
功能:删除集合S中所有跟S1中相同的元素。无相同元素时,各个集合不会发生改变。
Remove all elements of another set from this set.
语法:S.difference_update(S1)
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S2 = set('abcde')
>>> print S2
set(['a', 'c', 'b', 'e', 'd'])
>>> S3 = set('spiritman')
>>> print S3
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S1.difference_update(S2)
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> print S2
set(['a', 'c', 'b', 'e', 'd'])
>>> S1.difference_update(S3)
>>> print S1
set([])
>>> print S3
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S2.difference_update(S3)
>>> print S2
set(['c', 'b', 'e', 'd'])
>>> print S3
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
discard
功能:从集合中删除一个元素。一次只能删除一个。
Remove an element from a set if it is a member.
语法:S.discard(object)
实例展示:
S3 = set('spiritman')
>>> print S3
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S3.discard('a')
>>> print S3
set(['i', 'm', 'n', 'p', 's', 'r', 't'])
####################################################
#删除多个元素时报错
>>>S3.discard('i','m')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: discard() takes exactly one argument (2 given)
intersection
功能:求两个或多个集合的交集,交集返回一个新的集合。
Return the intersection of two or more sets as a new set.(elements that are common to all of the sets.)
语法:S.intersection(set1,set2...)
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S2 = set('spiman')
>>> print S2
set(['a', 'i', 'm', 'n', 'p', 's'])
>>> S3 = set('abcis')
>>> print S3
set(['a', 'i', 'c', 'b', 's'])
##################################################
#求S1和S2的交集
>>> S1.intersection(S2)
set(['i', 'p', 's'])
##################################################
#求S1和S3的交集
>>> S1.intersection(S3)
set(['i', 's'])
##################################################
#求S1、S2和S3的交集
>>> S1.intersection(S2,S3)
set(['i', 's'])
intersection_update
功能: 以一个集合和另一个集合的交集更新该集合。
Update a set with the intersection of itself and another.
语法:S.intersection_update(set1)
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S2 = set('spiman')
>>> print S2
set(['a', 'i', 'm', 'n', 'p', 's'])
>>> S3 = set('abcis')
>>> print S3
set(['a', 'i', 'c', 'b', 's'])
##################################################
#以S1和S3的交集更新S1
>>> S1.intersection_update(S3)
>>> print S1
set(['i', 's'])
##################################################
#以S2和S3的交集更新S2
>>> S2.intersection_update(S3)
>>> print S2
set(['a', 'i', 's'])
isdisjoint
功能:判断两个集合是否有集合。若没有则返回True;反之False。
Return True if two sets have a null intersection.
语法:S.isdisjoint(set1)
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S2 = set('spiman')
>>> print S2
set(['a', 'i', 'm', 'n', 'p', 's'])
>>> S3 = set('abcdef')
>>> print S3
set(['a', 'c', 'b', 'e', 'd', 'f'])
####################################################
>>> S1.isdisjoint(S2)
False
>>> S1.isdisjoint(S3)
True
>>> S2.isdisjoint(S3)
False
issubset
功能:判断集合是否是另一个集合的子集合。是则返回True;反之False.
Report whether another set contains this set.
语法:S.issubset(set1)
实例展示:
>>> S1 = set('spirit')
>>> print S1
set(['i', 'p', 's', 'r', 't'])
>>> S2 = set('spiritman')
>>> print S2
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S3 = set('abcis')
>>> print S3
set(['a', 'i', 'c', 'b', 's'])
##################################################
>>> S1.issubset(S2)
True
>>> S3.issubset(S2)
False
>>> S1.issubset(S3)
False
issuperset
功能:判断集合是否包含另一个集合。
Report whether this set contains another set
语法:issuperset(set1)
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S2 = set('spirit')
>>> print S2
set(['i', 'p', 's', 'r', 't'])
>>> S1.issuperset(S2)
True
pop
功能:删除并返回任意一个元素。
Remove and return an arbitrary set element.Raises KeyError if the set is empty.
语法:S.pop()
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't']) >>> S1.pop()
'a'
>>> S1.pop()
'i'
>>> S1.pop()
'm'
>>> print S1
set(['n', 'p', 's', 'r', 't'])
remove
功能:删除集合中一个指定的元素。
Remove an element from a set; it must be a member.If the element is not a member, raise a KeyError
语法:S.remove(object)
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S1.remove('n')
>>> print S1
set(['a', 'i', 'm', 'p', 's', 'r', 't'])
>>> S1.remove('b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'b'
symmetric_difference
功能:取两个集合的差集,返回一个新集合。
Return the symmetric difference of two sets as a new set.(all elements that are in exactly one of the sets.)
语法:S.symmetric_difference(set1)
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S2 = set('spirit')
>>> print S2
set(['i', 'p', 's', 'r', 't'])
>>> S3 = set('bcde')
>>> print S3
set(['c', 'b', 'e', 'd'])
##################################################
#返回S1和S2差集
>>> S1.symmetric_difference(S2)
set(['a', 'm', 'n'])
##################################################
#返回S1和S3差集
>>> S1.symmetric_difference(S3)
set(['a', 'c', 'b', 'e', 'd', 'i', 'm', 'n', 'p', 's', 'r', 't'])
##################################################
#返回S2和S3差集
>>> S2.symmetric_difference(S3)
set(['c', 'b', 'e', 'd', 'i', 'p', 's', 'r', 't'])
symmetric_difference_update
功能:取集合和另一个集合的差集,更新该集合。
Update a set with the symmetric difference of itself and another.
语法:symmetric_difference_update(set1)
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S2 = set('spirit')
>>> print S2
set(['i', 'p', 's', 'r', 't'])
>>> S3 = set('bcde')
>>> print S3
set(['c', 'b', 'e', 'd'])
####################################################
>>> S1.symmetric_difference_update(S2)
>>> print S1
set(['a', 'm', 'n']) >>> S2.symmetric_difference_update(S3)
>>> print S2
set(['c', 'b', 'e', 'd', 'i', 'p', 's', 'r', 't'])
union
功能:取多个集合的并集,返回一个新的集合。
Return the union of sets as a new set.(all elements that are in either set.)
语法:S.union(set1,set2....)
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S2 = set('spirit')
>>> print S2
set(['i', 'p', 's', 'r', 't'])
>>> S3 = set('bcde')
>>> print S3
set(['c', 'b', 'e', 'd'])
##################################################
#返回S1和S2的并集
>>> S1.union(S2)
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
##################################################
#返回S1和S3的并集
>>> S1.union(S3)
set(['a', 'c', 'b', 'e', 'd', 'i', 'm', 'n', 'p', 's', 'r', 't'])
#################################################
#返回S1、S2和S3的并集
>>> S1.union(S2,S3)
set(['a', 'c', 'b', 'e', 'd', 'i', 'm', 'n', 'p', 's', 'r', 't'])
update
功能:取一个集合和另一个集合的并集,更新该集合。
Update a set with the union of itself and others.
语法:S.update(set1)
实例展示:
>>> S1 = set('spiritman')
>>> print S1
set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
>>> S3 = set('bcde')
>>> print S3
set(['c', 'b', 'e', 'd'])
>>> S1.update(S3)
>>> print S1
set(['a', 'c', 'b', 'e', 'd', 'i', 'm', 'n', 'p', 's', 'r', 't'])
Python set 集合的更多相关文章
- Python 3 集合基础和概念!
Python 3 集合基础和概念! Python 3中,集合是无序的,所以不能进行切片和索引操作. 创建集合有两个方法:set()方法创建的集合是可变的,可被迭代的:frozenset()方法创建的集 ...
- Python的集合
1. Python的集合 1.1 集合的定义 在Python中, 集合set是基本数据类型的一种集合类型,它有可变集合(set())和不可变集合(frozenset)两种.Python中的集合set类 ...
- Python 操作集合
Python 操作集合 集合,set,主要用于数据的关系测试和去重处理,和列表类似,可以存储数据,列表中可以存储重复的数据,但是如果转化为集合之后,数据就会进行去重,然后保留唯一值:关系测试就是求多个 ...
- Python中集合set()的使用及处理
在Python中集合(set)与字典(dict)比较相似,都具有无序以及元素不能重复的特点 1.创建set 创建set需要一个list或者tuple或者dict作为输入集合 重复的元素在set中会被自 ...
- Python:集合操作总结
集合是一组无序排列的不重复元素集 [注]:集合的最大作用是对一个序列进行去重操作 一.集合的分类 在Python中集合分为两类,为可变集合(set)和不可变集合(frozenset).对于可变集合(s ...
- python 的集合 set()操作
Python 的集合 set(),是一个无序不重复元素集,可以用于关系测试和消除重复元素. 有以下运算: 1.创建一个set ()集合: 2.add:增加集合元素 3.clea ...
- python set集合(16)
在python变量中除了以前文章所提到的整形int / 浮点数float / 布尔值bool / 列表list / 字典dict 之外,还有一个类型我们还没有做详细介绍,这个变量类型就是集合set. ...
- python frozenset集合(17)
在前一篇文章中我们对 python set集合 做了详细的讲解,而本文讲解的 frozenset集合 其实和set集合类似!区别在于frozenset集合不能修改/添加/删除,其他功能和set集合一样 ...
- Python数据类型--集合(set)
Python的集合是无序.可迭代的容器对象,所有元素放在一对大括号中{},元素之间使用逗号隔开,同一集合内的元素具有唯一性,不允许重复. 集合中只能包含数字.字符串.元组等不可变类型的数据,不能包含列 ...
- [python]set集合学习
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...
随机推荐
- 20145203盖泽双实验一 Java开发环境的熟悉(Linux + Eclipse)
实验一 Java开发环境的熟悉(Linux + Eclipse) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.没 ...
- EF Core 入门
官方文档地址 https://docs.microsoft.com/zh-cn/aspnet/?view=aspnetcore-2.2#pivot=core EF Core 使用 1. 创建数据库上下 ...
- CentOS7.2安装mysql
1. 下载Mysql yum包 http://dev.mysql.com/downloads/repo/yum/ 复制链接使用wget下载 wget http://repo.mysql.com/mys ...
- Docker实战(一)之使用Docker镜像
镜像是Docker三大核心概念中最为重要的,自Docker诞生之日起“镜像”就是相关社区最为热门的关键字. Docker运行容器前需要本地存在对应的镜像,如果镜像没有保存至本地,Docker会尝试先从 ...
- Spring源码分析(十六)准备创建bean
本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 我们不可能指望在一个函数中完成一个复杂的逻辑,而且我们跟踪了这么多Spring ...
- 443 D. Teams Formation
http://codeforces.com/contest/879/problem/D This time the Berland Team Olympiad in Informatics is he ...
- C\C++ vector 构造函数 & 析构函数
#include <iostream> #include <vector> using namespace std; class Obj { public: Obj(void) ...
- SQL 练习一 字符型函数
处理字符串时,利用字符型函数的嵌套组合是非常有效的,试分析一道考题: create table customers(cust_name varchar2(20)); insert into custo ...
- Bugku Writeup —文件上传2(湖湘杯)
我们先来看下题目,题目说明是文件上传 我们可以尝试通过构造payload来进行测试 php://filter/read=convert.base64-encode/resource=flag 获取到f ...
- uva 1590 - IP Networks(IP地址)
习题4-5 IP网络(IP Networks, ACM/ICPC NEERC 2005, UVa1590) 可以用一个网络地址和一个子网掩码描述一个子网(即连续的IP地址范围).其中子网 掩码包含32 ...