集合的创建: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集合的更多相关文章

  1. Python 集合set添加删除、交集、并集、集合操作符号

    在Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法. 1. ...

  2. [转]python集合set

    Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法. 来源网 ...

  3. python集合使用范例的代码

    在代码过程中中,将代码过程中比较好的代码段珍藏起来,如下的代码是关于python集合使用范例的代码,希望能对大伙有用. # sets are unordered collections of uniq ...

  4. python集合与字典的用法

    python集合与字典的用法 集合: 1.增加  add 2.删除   •del 删除集合 •discard(常用)删除集合中的元素  #删除一个不存在的元素不会报错 •remove 删除一个不存在的 ...

  5. Python 集合内置函数大全(非常全!)

    Python集合内置函数操作大全 集合(s).方法名 等价符号 方法说明 s.issubset(t) s <= t 子集测试(允许不严格意义上的子集):s 中所有的元素都是 t 的成员   s ...

  6. Python 集合set()添加删除、交集、并集、集合操作详解

    集合:一个集合中,任何两个元素都认为是不相同的,即每个元素只能出现一次.每个元素的地位都是相同的,元素之间是无序的. 创建集合set python set类是在python的sets模块中,大家现在使 ...

  7. python集合可以进行相减

    python集合可以进行相减 student = {'tom','jim','mary','tom','jack','rose'} print(student) print('rose' in stu ...

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

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

  9. Python - 集合 - 第十一天

    Python 集合 集合(set)是一个无序的不重复元素序列. 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建 ...

  10. python集合set,交集,并集,差集,对称差集,子集和超集

    python集合set,交集,并集,差集,对称差集,子集和超集 x = {1, 2, 3, 4} y = {2, 4, 5, 6} # 交集(取x中与y中相同部分) print(x.intersect ...

随机推荐

  1. C++程序设计——知识点总结

    C++程序设计课程的总结,方便以后快速查阅和复习 Week 2 从C走进C++ 函数指针 函数名是函数的入口地址,指向函数的指针称为"函数指针". 比如,qsort库函数: voi ...

  2. SessionState

    SqlServer方式:1.创建数据库的方法:C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regsql -ssadd -sstype ...

  3. filterHTML

    function filterHTML(source) { return !source ? "" : source.replace(/]*>/g, "" ...

  4. 兼容IE6的min-width、min-height

    如果一个站是宽屏的,你左右拖动浏览器的窗口网站宽度会随着窗口的大小而改变,而浏览器窗口宽度减小到一定程度后就会出现下边的滚动条,网站宽度就不会再减小了,我们知道这一简单的功能用css的min-widt ...

  5. UIScrollView的常见属性

    @property(nonatomic) CGPoint contentOffset; 这个属性用来表示UIScrollView滚动的位置 (其实就是内容左上角与scrollView左上角的间距值) ...

  6. [Data Structure & Algorithm] 七大查找算法

    查找是在大量的信息中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找.本文简单概括性的介绍了常见的七种查找算法,说是七种,其实二分查找.插值查找以及斐波那契查找 ...

  7. poj 1141

    简单的dp 忘了\n,调了半天(目测不是第一次了) 直接贴代码: #include<cstdio> #include<cstring> using namespace std; ...

  8. java环境配置为1.7jdk为什么cmd java -version查看版本是1.8

    记录一个小问题: 初始安装的是jdk1.8,后来项目需要要更换成jdk1.7, 因此将环境变量更改为jdk7的目录路径, 但是在cmd命令行运行java -version 发现还是jdk8 解决方法: ...

  9. 群体结构图形三剑客——PCA图

    重测序便宜了,群体的测序和分析也多了起来.群体结构分析,是重测序最常见的分析内容.群体结构分析应用十分广泛,首先其本身是群体进化关系分析里面最基础的分析内容,其次在进行GWAS分析的时候,本身也需要使 ...

  10. Servlet 之 HttpServlet

    package cn.jiemoxiaodi.http; import java.io.IOException; import javax.servlet.GenericServlet; import ...