您的位置:首页 > 编程语言 > Python开发

python集合

2016-03-22 22:31 459 查看
集合的创建: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'}
>>>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: