集合的创建: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. Atom编辑器的插件

    先说下atom的插件安装方法吧,因为没用过苹果电脑,所以这里就只说下windows的操作吧. " ctrl+, "调出设置面板 点击install按钮,进去搜索插件面板 1.exp ...

  2. API接口:分页

    // 查询满足要求的总记录数 $count = M("back")->where($back_map)->count(); $pagecount = ceil($cou ...

  3. Verilog笔记——YUV2RGB的模块测试

    1 YUV2RGB的模块如下: module yuv2rgb( clk, //时钟输入 rstn, //复位输入,低电平复位 y_in, //变换前Y分量输出 cb_in, //变换前Cb分量输出 c ...

  4. HTML5 data-* 属性

    HTML5 data-* 属性 jQuery Mobile 依赖 HTML5 data-* 属性来支持各种 UI 元素.过渡和页面结构.不支持它们的浏览器将以静默方式弃用它们.表 2 显示如何使用 d ...

  5. ZOJ 3699 Dakar Rally

    Dakar Rally Time Limit: 2 Seconds      Memory Limit: 65536 KB Description The Dakar Rally is an annu ...

  6. Alpha版本十天冲刺——Day 5

    站立式会议 会前小侃:今天是双11,也是恰逢组内秋鑫同学生日,本组同学祝他双11生日快乐.天气好冷,注意保暖. 会议总结 队员 今天完成 遇到的问题 明天要做 感想 鲍亮 json数据解析学习,完成注 ...

  7. C和指针 第十二章 使用结构和指针 双链表和语句提炼

    双链表中每个节点包含指向当前和之后节点的指针,插入节点到双链表中需要考虑四种情况: 1.插入到链表头部 2.插入到链表尾部 3.插入到空链表中 4.插入到链表内部 #include <stdio ...

  8. python之路十一

    RabbitMQ基本概念RabbitMQ , 是一个使用 erlang 编写的 AMQP (高级消息队列协议) 的服务实现. 简单来说, 就是一个功能强大的消息队列服务.通常我们谈到队列服务, 会有三 ...

  9. jQuery UI Datepicker

    http://www.runoob.com/try/try.php?filename=jqueryui-example-datepicker-dropdown-month-year <!doct ...

  10. CSS 多类选择器

    写的代码多了,就会发现,自己越来越无知了,总以为html css很简单,已经掌握的很熟练了,其实我还差的很多. 平时没有用过css的这种写法  .a.b{display:block;}   上网一查才 ...