python 集合(set)
1.集合的创建
集合是一个无序不重复元素的集。基本功能包括关系测试和消除重复元素。
创建集合:大括号或 set() 函数可以用来创建集合。注意:想要创建空集合,你必须使用 set() 而不是 {},后者用于创建空字典。大括号也不可以创建元素含有字典与列表的集合。
- a={'a','b','c','d'}
- b=set('abcdefabcd')
- c=set({'a':1,'b':2})
- d=set(['a','b','c','a'])
- print(a,type(a))
- print(b,type(b))
- print(c,type(c))
- print(d,type(d))
- #运行结果
- {'c', 'd', 'b', 'a'} <class 'set'>
- {'f', 'e', 'b', 'c', 'd', 'a'} <class 'set'>
- {'b', 'a'} <class 'set'>
- {'c', 'b', 'a'} <class 'set'>
demo
2.集合的功能属性
- class set(object):
- """
- set() -> new empty set object
- set(iterable) -> new set object
- Build an unordered collection of unique elements.
- """
- 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
- def clear(self, *args, **kwargs): # real signature unknown
- """ Remove all elements from this set. """
- pass
- def copy(self, *args, **kwargs): # real signature unknown
- """ Return a shallow copy of a set. """
- pass
- 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
- def difference_update(self, *args, **kwargs): # real signature unknown
- """ Remove all elements of another set from this set. """
- pass
- 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
- 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
- def intersection_update(self, *args, **kwargs): # real signature unknown
- """ Update a set with the intersection of itself and another. """
- pass
- def isdisjoint(self, *args, **kwargs): # real signature unknown
- """ Return True if two sets have a null intersection. """
- pass
- def issubset(self, *args, **kwargs): # real signature unknown
- """ Report whether another set contains this set. """
- pass
- def issuperset(self, *args, **kwargs): # real signature unknown
- """ Report whether this set contains another set. """
- pass
- def pop(self, *args, **kwargs): # real signature unknown
- """
- Remove and return an arbitrary set element.
- Raises KeyError if the set is empty.
- """
- pass
- 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
- 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
- def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
- """ Update a set with the symmetric difference of itself and another. """
- pass
- 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
- def update(self, *args, **kwargs): # real signature unknown
- """ Update a set with the union of itself and others. """
- pass
- def __and__(self, *args, **kwargs): # real signature unknown
- """ Return self&value. """
- pass
- def __contains__(self, y): # real signature unknown; restored from __doc__
- """ x.__contains__(y) <==> y in x. """
- pass
- def __eq__(self, *args, **kwargs): # real signature unknown
- """ Return self==value. """
- pass
- def __getattribute__(self, *args, **kwargs): # real signature unknown
- """ Return getattr(self, name). """
- pass
- def __ge__(self, *args, **kwargs): # real signature unknown
- """ Return self>=value. """
- pass
- def __gt__(self, *args, **kwargs): # real signature unknown
- """ Return self>value. """
- pass
- def __iand__(self, *args, **kwargs): # real signature unknown
- """ Return self&=value. """
- pass
- def __init__(self, seq=()): # known special case of set.__init__
- """
- set() -> new empty set object
- set(iterable) -> new set object
- Build an unordered collection of unique elements.
- # (copied from class doc)
- """
- pass
- def __ior__(self, *args, **kwargs): # real signature unknown
- """ Return self|=value. """
- pass
- def __isub__(self, *args, **kwargs): # real signature unknown
- """ Return self-=value. """
- pass
- def __iter__(self, *args, **kwargs): # real signature unknown
- """ Implement iter(self). """
- pass
- def __ixor__(self, *args, **kwargs): # real signature unknown
- """ Return self^=value. """
- pass
- def __len__(self, *args, **kwargs): # real signature unknown
- """ Return len(self). """
- pass
- def __le__(self, *args, **kwargs): # real signature unknown
- """ Return self<=value. """
- pass
- def __lt__(self, *args, **kwargs): # real signature unknown
- """ Return self<value. """
- pass
- @staticmethod # known case of __new__
- def __new__(*args, **kwargs): # real signature unknown
- """ Create and return a new object. See help(type) for accurate signature. """
- pass
- def __ne__(self, *args, **kwargs): # real signature unknown
- """ Return self!=value. """
- pass
- def __or__(self, *args, **kwargs): # real signature unknown
- """ Return self|value. """
- pass
- def __rand__(self, *args, **kwargs): # real signature unknown
- """ Return value&self. """
- pass
- def __reduce__(self, *args, **kwargs): # real signature unknown
- """ Return state information for pickling. """
- pass
- def __repr__(self, *args, **kwargs): # real signature unknown
- """ Return repr(self). """
- pass
- def __ror__(self, *args, **kwargs): # real signature unknown
- """ Return value|self. """
- pass
- def __rsub__(self, *args, **kwargs): # real signature unknown
- """ Return value-self. """
- pass
- def __rxor__(self, *args, **kwargs): # real signature unknown
- """ Return value^self. """
- pass
- def __sizeof__(self): # real signature unknown; restored from __doc__
- """ S.__sizeof__() -> size of S in memory, in bytes """
- pass
- def __sub__(self, *args, **kwargs): # real signature unknown
- """ Return self-value. """
- pass
- def __xor__(self, *args, **kwargs): # real signature unknown
- """ Return self^value. """
- pass
- __hash__ = None
set
3.集合的部分功能属性介绍
1)add(self, *args, **kwargs):
在集合里添加一个元素,不生成新的集合。
- a={'a','b','c','d'}
- b=a.add('e')
- c=a.add('a')
- print(a,type(a))
- print(b,type(b))
- print(c,type(c))
- #运行结果
- {'d', 'c', 'e', 'b', 'a'} <class 'set'>
- None <class 'NoneType'>
- None <class 'NoneType'>
demo
2)clear(self, *args, **kwargs):
清空集合里面的元素,不生成新的集合。
- a={'a','b','c','d'}
- b=a.clear()
- print(a,type(a))
- print(b,type(b))
- #运行结果
- set() <class 'set'>
- None <class 'NoneType'>
demo
3)copy(self, *args, **kwargs):
浅拷贝集合,返回一个新集合。
- a={1,(9,2),3}
- b=a.copy()
- print(a,id(a))
- print(b,id(b))
- #赋值
- c={1,2,3,4}
- d=c
- print(c,id(c))
- print(d,id(d))
- #运行结果
- {(9, 2), 1, 3} 13058696
- {(9, 2), 1, 3} 13058576
- {1, 2, 3, 4} 13059296
- {1, 2, 3, 4} 13059296
demo
4)difference(self, *args, **kwargs):
与其他一个集合或多个集合对比,返回一个与其他集合不一样元素的集合。
- a={'a','b','c','d'}
- b=a.difference({'a',},{'b'})
- print(a)
- print(b,type(b))
- #运行结果
- {'c', 'b', 'a', 'd'}
- {'c', 'd'} <class 'set'>
demo
5)difference_update(self, *args, **kwargs):
与其他一个集合或多个集合对比,删除集合与其他集合一样的元素,不返回新集合。
- a={'a','b','c','d'}
- b=a.difference_update({'a',},{'b'})
- print(a)
- print(b,type(b))
- #运行结果
- {'c', 'd'}
- None <class 'NoneType'>
demo
6)discard(self, *args, **kwargs):
删除集合中的某个元素,如果这个元素没有在集合中,不做操作,不返回新集合。
- a={'a','b','c','d'}
- b=a.discard('a')
- print(a)
- print(b,type(b))
- #运行结果
- {'d', 'c', 'b'}
- None <class 'NoneType'>
demo
7)intersection(self, *args, **kwargs):
返回一个和其他集合共同有的元素的集合。
- a={'a','b','c','d'}
- b=a.intersection({'a','e'},{'a','f'})
- print(a)
- print(b,type(b))
- #运行结果
- {'d', 'b', 'c', 'a'}
- {'a'} <class 'set'>
demo
8)intersection_update(self, *args, **kwargs):
与其他一个集合或多个集合对比,删除集合中与其他集合不共有的元素,不返回新集合。
- a={'a','b','c','d'}
- b=a.intersection_update({'a','e'},{'a','f'})
- print(a)
- print(b,type(b))
- #运行结果
- {'a'}
- None <class 'NoneType'>
demo
9)isdisjoint(self, *args, **kwargs):
对比两个集合,有空交集则返回True,没有则返回False。
- a={'a','b','c','d',' '} #空元素用tab键输入
- b=a.isdisjoint({'a','e'})
- c=a.isdisjoint({' ','f'})
- print(a)
- print(b,type(b))
- print(c,type(c))
- #运行结果
- {'a', 'b', ' ', 'c', 'd'}
- False <class 'bool'>
- True <class 'bool'>
demo
10)issubset(self, *args, **kwargs):
判断集合的包含关系,其他集合如果包含原集合则返回True,不包含则返回Fasle。
- a={'a','b','c','d',}
- b={'a','b','c','d','f'}
- c=a.issubset(b)
- d=a.issubset({'a'})
- print(a)
- print(b)
- print(c,type(c))
- print(d,type(d))
- #运行结果
- {'d', 'a', 'b', 'c'}
- {'f', 'b', 'a', 'd', 'c'}
- True <class 'bool'>
- False <class 'bool'>
demo
11)issuperset(self, *args, **kwargs):
判断集合的包含关系,原集合如果包含其他集合则返回True,不包含则返回Fasle。
- a={'a','b','c','d',}
- b={'a','b','c','d','f'}
- c=a.issuperset(b)
- d=a.issuperset({'a'})
- print(a)
- print(b)
- print(c,type(c))
- print(d,type(d))
- #运行结果
- {'a', 'b', 'c', 'd'}
- {'a', 'b', 'd', 'c', 'f'}
- False <class 'bool'>
- True <class 'bool'>
demo
12)pop(self, *args, **kwargs):
从集合中取出一个元素,如果集合为空,则报TypeError错误。
- a={'a','b','c','d',}
- b=a.pop()
- print(a)
- print(b,type(b))
- #运行结果
- {'c', 'b', 'a'}
- d <class 'str'> #因为集合是无序的,所以取出的值是随机的
demo
13)remove(self, *args, **kwargs):
移除集合中的一个元素,这个元素必须在集合中,如果不在,则报TypeError错误。
- a={'a','b','c','d',}
- b=a.remove('b')
- print(a)
- print(b,type(b))
- #运行结果
- {'c', 'a', 'd'}
- None <class 'NoneType'>
demo
14)union(self, *args, **kwargs):
两个集合拼接返回一个新集合。
- a={'a','b','c','d',}
- b=a.union('b')
- c=a.union({'e','f'})
- print(a)
- print(b,type(b))
- print(c,type(c))
- #运行结果
- {'b', 'd', 'c', 'a'}
- {'b', 'd', 'c', 'a'} <class 'set'>
- {'d', 'c', 'e', 'b', 'f', 'a'} <class 'set'>
demo
15)update(self, *args, **kwargs):
更新集合,添加集合中没有的新元素,不返回新集合。
- a={'a','b','c','d',}
- b=a.update('b')
- c=a.update({'e','f'})
- print(a)
- print(b,type(b))
- print(c,type(c))
- #运行结果
- {'a', 'c', 'b', 'e', 'd', 'f'}
- None <class 'NoneType'>
- None <class 'NoneType'>
demo
16)__and__(self, *args, **kwargs):
和intersection()一样,返回一个和其他集合共同有的元素的集合,但是前者可以和多个集合一起对比,后者只可以和一个集合对比。
- a={'a','b','c','d',}
- b=a.__and__('b')
- c=a.__and__({'a','f'})
- print(a)
- print(b,type(b))
- print(c,type(c))
- #运行结果
- {'a', 'b', 'd', 'c'}
- NotImplemented <class 'NotImplementedType'>
- {'a'} <class 'set'>
demo
17)__contains__(self, y):
判断集合中有没有包含某个元素或集合,包含则返回True,不包含则返回Fasle。
- a={'a','b','c','d',}
- b=a.__contains__('b')
- c=a.__contains__({'a','f'})
- print(a)
- print(b,type(b))
- print(c,type(c))
- #运行结果
- {'a', 'd', 'c', 'b'}
- True <class 'bool'>
- False <class 'bool'>
demo
python 集合(set)的更多相关文章
- Python 集合set添加删除、交集、并集、集合操作符号
在Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法. 1. ...
- [转]python集合set
Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法. 来源网 ...
- python集合使用范例的代码
在代码过程中中,将代码过程中比较好的代码段珍藏起来,如下的代码是关于python集合使用范例的代码,希望能对大伙有用. # sets are unordered collections of uniq ...
- python集合与字典的用法
python集合与字典的用法 集合: 1.增加 add 2.删除 •del 删除集合 •discard(常用)删除集合中的元素 #删除一个不存在的元素不会报错 •remove 删除一个不存在的 ...
- Python 集合内置函数大全(非常全!)
Python集合内置函数操作大全 集合(s).方法名 等价符号 方法说明 s.issubset(t) s <= t 子集测试(允许不严格意义上的子集):s 中所有的元素都是 t 的成员 s ...
- Python 集合set()添加删除、交集、并集、集合操作详解
集合:一个集合中,任何两个元素都认为是不相同的,即每个元素只能出现一次.每个元素的地位都是相同的,元素之间是无序的. 创建集合set python set类是在python的sets模块中,大家现在使 ...
- python集合可以进行相减
python集合可以进行相减 student = {'tom','jim','mary','tom','jack','rose'} print(student) print('rose' in stu ...
- Python集合类型的操作与应用
Python集合类型的操作与应用 一.Python集合类型 Python中的集合类型是一个包含0个或多个数据项的无序的.不重复的数据组合,其中,元素类型只能是固定数据类型,如整数.浮点数.字符串.元组 ...
- Python - 集合 - 第十一天
Python 集合 集合(set)是一个无序的不重复元素序列. 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建 ...
- python集合set,交集,并集,差集,对称差集,子集和超集
python集合set,交集,并集,差集,对称差集,子集和超集 x = {1, 2, 3, 4} y = {2, 4, 5, 6} # 交集(取x中与y中相同部分) print(x.intersect ...
随机推荐
- SQL Injection简介
- ES6形参默认值
* 形参的默认值----当不传入参数的时候默认使用形参里的默认值 function Point(x = ,y = ) { this.x = x; this.y = y; } //定义一个点的坐标 fu ...
- Jvm之class文件的加载、初始化
编写的java文件在要真正运行时,会首先被编译成 “.class"结尾的二进制文件,然后被虚拟机加载.那么在虚拟机中一个class文件要成为java实例,需要经历好几个步骤: 一.class ...
- bootstrap 基础模板相关信息
<!DOCTYPE html> <html> <head> <title></title> <link rel="style ...
- Js数据类型和运算符
1.数据类型 原始类型:数值 字符串 布尔值: 复杂类型:对象: 数值(number) 特殊的数值:NaN,NaN不等于任何 ...
- Ruby 类和对象
Ruby 类和对象 Ruby 是一种完美的面向对象编程语言.面向对象编程语言的特性包括: 数据封装 数据抽象 多态性 继承 这些特性将在 面向对象的 Ruby 中进行讨论. 一个面向对象的程序,涉及到 ...
- 2018年阿里云NoSQL数据库大事盘点
NoSQL一词最早出现在1998年.2009年Last.fm的Johan Oskarsson发起了一次关于分布式开源数据库的讨论,来自Rackspace的Eric Evans再次提出了NoSQL概念, ...
- php上传(一)
php上传基础知识总结 1.文件上传 1>文件上传的原理:将客户端上的文件通过浏览器上传到服务器上, 在将服务器上的文件移动到指定目录. 2>客户端的配置 表单页面,浏览框 method= ...
- TopCoder[SRM587 DIV 1]:ThreeColorability(900)
Problem Statement There is a H times W rectangle divided into unit cells. The rows of cells are ...
- NX二次开发-获取按钮的ID UF_MB_ask_button_id
NX9+VS2012 1.打开D:\Program Files\Siemens\NX 9.0\UGII\menus\ug_main.men 找到装配和PMI,在中间加上一段 TOGGLE_BUTTON ...