python集合
集合的创建:set()和frozenset()
区别:frozenset()创建不可变的集合,一旦创建其元素不可改变;而set()创建的集合中的元素可以通过一定的方法进行改变。
>>> frozenset('chinese')
frozenset({'c', 'h', 's', 'e', 'n', 'i'})
>>> set('chinese')
{'c', 'h', 's', 'e', 'n', 'i'}
>>> type(set('chinese'))
<class 'set'>
>>> type(frozenset('chinese'))
<class 'frozenset'>
>>> len(frozenset('chinese'))
6
>>> len(set('chinese'))
6
>>> frozenset('chinese')==set('chinese')
True
>>>
>>> a = set('hello')
>>> a
{'h', 'e', 'l', 'o'}
>>> b = set(['hello',123,123,4])
>>> b
{'hello', 123, 4}
>>> c = frozenset('hello')
>>> c
frozenset({'h', 'e', 'l', 'o'})
>>> d = frozenset(['hello',123,123,4])
>>> d
frozenset({'hello', 123, 4})
>>> e = set(('hello',123,123,4))
>>> e
{'hello', 123, 4}
>>> f = frozenset(('hello',123,123,4))
>>> f
frozenset({'hello', 123, 4})
>>>
访问集合中的值
>>> a = set('hello')
>>> a
{'h', 'o', 'l', 'e'}
>>> 'e' in a
True
>>> 'g' in a
False
>>> 'g' not in a
True
>>> for i in a:print(i)
h
o
l
e
>>>
更新集合:
追加
>>> a = set('hello')
>>> a
{'h', 'e', 'l', 'o'}
>>> a.add('zyj')
>>> a
{'h', 'e', 'l', 'o', 'zyj'}
>>> a1 = set('zyj')
>>> a.add(a1)
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
a.add(a1)
TypeError: unhashable type: 'set'
>>> a.add(['zyj','!'])
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
a.add(['zyj','!'])
TypeError: unhashable type: 'list'
>>> a.add(('zyj','!'))
>>> a
{'zyj', 'h', 'l', 'o', 'e', ('zyj', '!')}
>>>
>>> c.add('zyj')
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
c.add('zyj')
AttributeError: 'frozenset' object has no attribute 'add'
>>>
更新:
>>> x = set('hello')
>>> x
{'h', 'e', 'l', 'o'}
>>> x.update('zyj')
>>> x
{'h', 'l', 'j', 'y', 'o', 'e', 'z'}
>>> x.add('zyj')
>>> x
{'zyj', 'h', 'l', 'j', 'y', 'o', 'e', 'z'}
>>>
移除:
>>> x
{'zyj', 'h', 'l', 'j', 'y', 'o', 'e', 'z'}
>>> x.remove('zyj')
>>> x
{'h', 'l', 'j', 'y', 'o', 'e', 'z'}
>>> x.pop('l')
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
x.pop('l')
TypeError: pop() takes no arguments (1 given)
>>> x.pop() #删除任意一个对象并返回它
'h'
>>> b
{'j', 'y'}
>>> b.discard('sl')#删除一个元素,元素不存在时不会报错
>>> b
{'j', 'y'}
>>> b.remove('sl')#删除一个元素,元素不存在时会报错
Traceback (most recent call last):
File "<pyshell#55>", line 1, in <module>
b.remove('sl')
KeyError: 'sl'
>>>
删除集合:
>>> del(x)
>>> x
Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
x
NameError: name 'x' is not defined
>>> a = set('zyj')
>>> a
{'z', 'j', 'y'}
>>> a.clear()#清除集合中的所有元素,之前的集合变为空集合
>>> a
set()
集合类型操作符:
集合等价、不等价(==、!=)
>>> a = set('hello')
>>> a
{'l', 'e', 'o', 'h'}
>>> b =frozenset('helo')
>>> b
frozenset({'l', 'e', 'o', 'h'})
>>> a == b
True
>>> a != b
False
>>>
子集、超集(<、<=、>、>=):s.issubset(t) s.issuperset(t)
>>> set('he') <= set('hello')
True
>>> set('helor') <set('hello')
False
>>> set('helor') >= set('hello')
True
>>> frozenset(set('hello'))
frozenset({'l', 'e', 'o', 'h'})
>>> set('he') == set('eh')
True
>>>
>>> a =set('hi')
>>> a
{'i', 'h'}
>>> b
{'l', 'e', 'o', 'h'}
>>> a.issubset(b)
False
>>> b.issuperset(a)
False
>>> c = set('h')
>>> c
{'h'}
>>> b
{'l', 'e', 'o', 'h'}
>>> c.issubset(b)
True
>>> b.issuperset(c)
True
>>>
集合类型操作符
联合符号(|):两个集合联合生成一个新集合,该集合中的每个元素都至少是其中一个集合的成员,联合符号等价的方法为union()
>>> set([1,2,3]) | set('hello')
{1, 2, 3, 'l', 'e', 'o', 'h'}
>>> set([1,2,3]).union(set('hello'))
{1, 2, 3, 'l', 'e', 'o', 'h'}
交集(&):两个集合联合生成一个新集合,该集合中的每个元素同时是两个集合的成员,交集符号等价的方法为intersection()
>>> set([1,2,3]) & set('hello')
set()
>>> set('hello') & set('hi')
{'h'}
>>> set('hello').intersection(set('hi'))
{'h'}
>>>
差补/相对补集(-):指两个集合之间的差。等价方法为:difference()
>>> set('hello')-set('h')
{'l', 'e', 'o'}
>>> set('h')-set('hello')
set()
>>> set('hello').difference(set('h'))
{'l', 'e', 'o'}
>>>
对称差分(^):生成集合中的元素不能同时属于两个集合,等价方法为:symmetric_difference()
>>> set('hello')^set('hi')
{'l', 'i', 'e', 'o'}
>>> set('hello').symmetric_difference('hi')
{'l', 'i', 'e', 'o'}
>>>
注意:+不属于集合的操作符。
>>> set('hello')+set('h')
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
set('hello')+set('h')
TypeError: unsupported operand type(s) for +: 'set' and 'set'
>>>
s.copy():返回一个新集合。
>>> a
{'l', 'e', 'o', 'h'}
>>>
>>> id(a)
49102280
>>> b = a.copy()
>>> id(b)
51347984
>>> del(a)
>>> a
Traceback (most recent call last):
File "<pyshell#112>", line 1, in <module>
a
NameError: name 'a' is not defined
>>> b
{'l', 'e', 'o', 'h'}
>>>
适用于可变集合的方法:
联合更新(|=):update() 对原集合进行更新
>>> a= set('hello')
>>> a
{'l', 'e', 'o', 'h'}
>>> b = set('hi')
>>> b
{'i', 'h'}
>>> a |= b
>>> a
{'l', 'e', 'o', 'i', 'h'}
>>> c = frozenset('world')
>>> a |= c
>>> a
{'l', 'w', 'e', 'o', 'i', 'r', 'd', 'h'}
>>> c |= a
>>> c
frozenset({'w', 'o', 'r', 'd', 'l', 'e', 'i', 'h'})
>>> a
{'l', 'w', 'e', 'o', 'i', 'r', 'd', 'h'}
>>> c.update(a)
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
c.update(a)
AttributeError: 'frozenset' object has no attribute 'update'
>>> a
{'i', 'h'}
>>> a.update(c)
>>> a
{'i', 'h'}
交集更新(&=):intersection_update()
>>> a
{'l', 'w', 'e', 'o', 'i', 'r', 'd', 'h'}
>>> b
{'i', 'h'}
>>> a &= b
>>> a
{'i', 'h'}
>>> c
frozenset({'w', 'o', 'r', 'd', 'l', 'e', 'i', 'h'})
>>> a &= c
>>> a
{'i', 'h'}
>>> c &= a
>>> c
frozenset({'i', 'h'})
>>> a = set('hello')
>>> a
{'l', 'e', 'o', 'h'}
>>> b = set('hi')
>>> b
{'i', 'h'}
>>> a.update(b)
>>> a
{'l', 'e', 'o', 'i', 'h'}
>>> a.intersection_update(b)
>>> a
{'i', 'h'}
>>>
差分更新(-=):difference_update()
>>> a = set('hello')
>>> a
{'l', 'e', 'o', 'h'}
>>> b =set('hi')
>>> b
{'i', 'h'}
>>> a -= b
>>> a
{'l', 'e', 'o'}
>>> b.difference_update(a)
>>> b
{'i', 'h'}
>>>
^=:symmetric_difference_update()
>>> a = set('hello')
>>> a
{'l', 'e', 'o', 'h'}
>>> b =set('hi')
>>> b
{'i', 'h'}
>>> a ^= b
>>> a
{'l', 'e', 'o', 'i'}
>>> a.symmetric_difference_update(b)
>>> a
{'l', 'e', 'o', 'h'}
>>>
python集合的更多相关文章
- 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 ...
随机推荐
- diff和patch的使用、patch文件的格式解说
为了弄懂 patch中的 p0 p1 和.orig文件是啥,找到了这篇文章! 来源:http://www.cnblogs.com/super119/archive/2010/12/18/19 ...
- linux shell脚本查找重复行/查找非重复行/去除重复行/重复行统计
转自:http://blog.sina.com.cn/s/blog_6797a6700101pdm7.html 去除重复行 sort file |uniq 查找非重复行 sort file |uniq ...
- 用C#开发ActiveX控件,并使用web调用
入职差不多两个月了,由学生慢慢向职场人做转变,也慢慢的积累知识,不断的更新自己.最近的一个项目里边,涉及到的一些问题,因为SDK提供的只是winform才能使用了,但是有需求咱们必须得完成啊,所以涉及 ...
- R语言-Kindle特价书爬榜示例 & 输出HTML小技巧
博客总目录:http://www.cnblogs.com/weibaar/p/4507801.html ---- 自从买了kindle以后,总是想要定期刷有没有便宜的书,amazon经常有些1元/2元 ...
- window共享linux下的文件 samba
1.在Ubuntu上安装samba服务 sudo apt-get install samba 2.修改配置文件vim /etc/samba/smb.conf [xubu] (共享名) guest ac ...
- XHR——XMLHttpRquest对象
创建XMLHttpRequest对象 与之前众多DOM操作一样,创建XHR对象也具有兼容性问题:IE6及之前的版本使用ActiveXObject,IE7之后及其它浏览器使用XMLHttpRequest ...
- 通过goto语句学习if...else、switch语句并简单优化
goto语句在C语言中实现的就是无条件跳转,第二章一上来就介绍goto语句就是要通过goto语句来更加清楚直观的了解控制结构. 我理解的goto语句其实跟switch语句有相似之处,都是进行跳转.不同 ...
- angular-ui-bootstrap-modal必须要说的几个点(转)
angular-ui-bootstrap-modal必须要说的几个点 摘要: 基于angular来实现的一个bootstrap模态框,有些不得不说的地方 项目中以前就经常用到模态框,但是一直没有时间来 ...
- 上个项目的一些反思 II
上个项目需要使用通讯录,我在回顾自己设计的时候,发现自己少设计了cache这一环. 虽然直接用SQLite在初期体验上没什么大损失,不过可以预想通讯录增长到一定数量后势必会影响体验. 单例模式,全局缓 ...
- 设计模式--工厂模式Factory(创建型)
工厂模式属于创建型模式,分为三类,简单工厂模式.工厂方法模式.抽象工厂模式. 一.简单工厂模式 在工厂中做判断,根据产品类型从而创造相应的产品,当增加新产品时需要修改工厂类. 例如: enum CTY ...