SET集合
特点:
无序
元素不重复(可以用于海量数据去重)
功能
关系测试
去重
==================================
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素.
集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对
称差集)等数学运算.
==================================
>>> name_set = set([1,2,4,5])
>>> type(name_set)
<type 'set'>
>>> name_set
set([1, 2, 4, 5]) 集合类型
>>> name_set = set([1,2,4,5,2])
>>> name_set
set([1, 2, 4, 5]) 这里并没有新加的2,天然去重复
>>> a = range(10)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a = set(a) 把列表变成集合类型
>>> type(a)
<type 'set'>
不支持索引
>>> a[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing 不支持索引
添加数据
>>> a.add(1)
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a.add(100)
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100])
>>> a.add(-1)
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, -1])
删除数据
>>> a.pop()
0
>>> a
set([1, 2, 3, 4, 5, 6, 7, 8, 9, 100, -1])
>>> a.pop()
1
>>> a
set([2, 3, 4, 5, 6, 7, 8, 9, 100, -1])
-------------------------
测试x、y的关系
>>> x = set([1,2,3,4])
>>> y = set([3,4,5,6])
交集(x.intersection(y))
>>> x & y
set([3, 4])
并集(x.union(y))
>>> x | y
set([1, 2, 3, 4, 5, 6])
差集(x.difference(y))
>>> x - y x里存在,y里不存在
set([1, 2])
对称差集(反差集)(x.symmetric_difference(y)) 他俩都有的去掉,都没有的合到一块
>>> x ^ y
set([1, 2, 5, 6])
子集(z.issubset(x))
>>> x,y 都不完全包含
(set([1, 2, 3, 4]), set([3, 4, 5, 6]))
>>> z = set([1,2,4])
>>> z.issubset(x)
True
父集(z.issuperset(x))
>>> z.issuperset(x)
False
=================================
集合添加、删除
python 集合的添加有两种常用方法,分别是add和update。
集合add方法:是把要传入的元素做为一个整个添加到集合中,例如:
>>> a = set('boy')
>>> a.add('python')
>>> a
set(['y', 'python', 'b', 'o'])
集合update方法:是把要传入的元素拆分,做为个体传入到集合中,例如:
>>> a = set('boy')
>>> a.update('python')
>>> a
set(['b', 'h', 'o', 'n', 'p', 't', 'y'])
集合删除操作方法:remove
set(['y', 'python', 'b', 'o'])
>>> a.remove('python')
>>> a
set(['y', 'b', 'o'])
=================================
sets 支持 x in set, len(set),和 for x in set。作为一个无序的集合,sets不记录元素位置
或者插入点。因此,sets不支持 indexing, slicing, 或其它类序列(sequence-like)的操作。
特点:
无序
元素不重复(可以用于海量数据去重)
功能
关系测试
去重
==================================
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素.
集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对
称差集)等数学运算.
==================================
>>> name_set = set([1,2,4,5])
>>> type(name_set)
<type 'set'>
>>> name_set
set([1, 2, 4, 5]) 集合类型
>>> name_set = set([1,2,4,5,2])
>>> name_set
set([1, 2, 4, 5]) 这里并没有新加的2,天然去重复
>>> a = range(10)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a = set(a) 把列表变成集合类型
>>> type(a)
<type 'set'>
不支持索引
>>> a[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing 不支持索引
添加数据
>>> a.add(1)
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a.add(100)
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100])
>>> a.add(-1)
>>> a
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, -1])
删除数据
>>> a.pop()
0
>>> a
set([1, 2, 3, 4, 5, 6, 7, 8, 9, 100, -1])
>>> a.pop()
1
>>> a
set([2, 3, 4, 5, 6, 7, 8, 9, 100, -1])
-------------------------
测试x、y的关系
>>> x = set([1,2,3,4])
>>> y = set([3,4,5,6])
交集(x.intersection(y))
>>> x & y
set([3, 4])
并集(x.union(y))
>>> x | y
set([1, 2, 3, 4, 5, 6])
差集(x.difference(y))
>>> x - y x里存在,y里不存在
set([1, 2])
对称差集(反差集)(x.symmetric_difference(y)) 他俩都有的去掉,都没有的合到一块
>>> x ^ y
set([1, 2, 5, 6])
子集(z.issubset(x))
>>> x,y 都不完全包含
(set([1, 2, 3, 4]), set([3, 4, 5, 6]))
>>> z = set([1,2,4])
>>> z.issubset(x)
True
父集(z.issuperset(x))
>>> z.issuperset(x)
False
=================================
集合添加、删除
python 集合的添加有两种常用方法,分别是add和update。
集合add方法:是把要传入的元素做为一个整个添加到集合中,例如:
>>> a = set('boy')
>>> a.add('python')
>>> a
set(['y', 'python', 'b', 'o'])
集合update方法:是把要传入的元素拆分,做为个体传入到集合中,例如:
>>> a = set('boy')
>>> a.update('python')
>>> a
set(['b', 'h', 'o', 'n', 'p', 't', 'y'])
集合删除操作方法:remove
set(['y', 'python', 'b', 'o'])
>>> a.remove('python')
>>> a
set(['y', 'b', 'o'])
=================================
sets 支持 x in set, len(set),和 for x in set。作为一个无序的集合,sets不记录元素位置
或者插入点。因此,sets不支持 indexing, slicing, 或其它类序列(sequence-like)的操作。