Python集合

一、集合简介

集合是一个无序的,不重复的数据组合。
集合的应用场景主要有:

  • 列表去重
  • 关系测试:如交集、差集、并集的关系测试

二、集合的定义

1.定义一个空集合
 In [4]: s = {}

 In [5]: type(s)     #发现是字典
 Out[5]: dict

 In [6]: s = set()

 In [7]: type(s)     #发现是集合
 Out[7]: set
2.定义一个有元素的集合
 In [10]: s = {1,2,3,4,'s'}

 In [11]: s
 Out[11]: {1, 2, 3, 4, 's'}

 In [12]: type(s)
 Out[12]: set

 In [13]: set = {1,2,3,'hello',(1,2,3)}  #集合元素可以是元组

 In [14]: set
 Out[14]: {1, 2, 3, 'hello', (1, 2, 3)}

 In [15]: set = {1,2,3,'hello',(1,2,3),[1,2,3]}  #发现集合的元素不能为列表
 ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
 15-9b6dfb40f232> in <module>()
 ----> 1 set = {1,2,3,'hello',(1,2,3),[1,2,3]}

 TypeError: unhashable type: 'list'

三、集合的操作

1.与字典类似:
  • 集合集不支持的特性:索引, 切片, 重复,连接
  • 集合支持的特性:成员操作符
 In [16]: set
 Out[16]: {1, 2, 3, 'hello', (1, 2, 3)}

 In [17]: set[1]
 ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
 input-17-3c9dd01669e4> in <module>()
 ----> 1 set[1]

 TypeError: 'set' object does not support indexing

 In [18]: set[1:3]
 ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
 input-18-fd2069647220> in <module>()
 ----> 1 set[1:3]

 TypeError: 'set' object has no attribute '__getitem__'

 In [19]: set * 2
 ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
 input-19-8242026add3c> in <module>()
 ----> 1 set * 2

 TypeError: unsupported operand type(s) for *: 'set' and 'int'

 In [20]: s
 Out[20]: {1, 2, 3, 4, 's'}

 In [21]: s + set
---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
 input-21-2e35e8aae9b8> in <module>()
 ----> 1 s + set

 TypeError: unsupported operand type(s) for +: 'set' and 'set'


 In [22]: set
 Out[22]: {1, 2, 3, 'hello', (1, 2, 3)}

 In [23]: 2 in set
 Out[23]: True

 In [24]: 4 in set
 Out[24]: False

 In [25]: (1,2,3) not in set
 Out[25]: False
2.此外,集合是无序的,可迭代的数据类型
In [26]: set
Out[26]: {1, 2, 3, 'hello', (1, 2, 3)}

In [27]: set.add(1+2j)   #发现并不是追加为set最后一个元素,说明set是无序数据类型

In [28]: set
Out[28]: {1, 2, 3, (1+2j), (1, 2, 3), 'hello'}


 In [29]: for i in set:   #set的迭代
    ....:     print i
    ....:     
 1
 2
 3
 (1+2j)
 (1, 2, 3)
 hello
3.集合的长度
 In [36]: set
 Out[36]: {1, 2, 3, (1+2j), (1, 2, 3), 'hello'}

 In [37]: len(set)
 Out[37]: 6
4.字典转换为集合

需要注意的是,ipython并不支持此操作,需要在PyCharm中完成

 d = dict(a = 1, b = 2, c = 3)
 print set(d)
 ##################输出的结果#################
 set(['a', 'c', 'b'])

四、集合的内置方法

1.集合的所有内置方法
 In [36]: set.
 set.add                                     set.issubset
 set.clear                                   set.issuperset
 set.copy                                   set.pop
 set.difference                          set.remove
 set.difference_update            set.symmetric_difference
 set.discard                                set.symmetric_difference_update
 set.intersection                        set.union
 set.intersection_update          set.update
 set.isdisjoint 
2.集合内置方法使用
1)集合的增删
 In [57]: set1
 Out[57]: {1, 2, 3}

 In [58]: set1.add(5)         #添加一项

 In [59]: set1
 Out[59]: {1, 2, 3, 5}

 In [60]: set1.update({3,4,5,6})  #添加多项,接可迭代数据类型

 In [61]: set1
 Out[61]: {1, 2, 3, 4, 5, 6}

 In [62]: set1.update('hello') #发现只有一个l,集合的元素不可重复

 In [63]: set1
 Out[63]: {1, 2, 3, 4, 5, 6, 'e', 'h', 'l', 'o'}

 In [64]: set1.update([1,2,10,20])

 In [65]: set1
 Out[65]: {1, 2, 3, 4, 5, 6, 10, 20, 'e', 'h', 'l', 'o'}

删:

 In [65]: set1
 Out[65]: {1, 2, 3, 4, 5, 6, 10, 20, 'e', 'h', 'l', 'o'}

 In [66]: set1.remove(3)

 In [67]: set1
 Out[67]: {1, 2, 4, 5, 6, 10, 20, 'e', 'h', 'l', 'o'}

 In [68]: set1.pop()
 Out[68]: 1

 In [69]: set1.pop()
 Out[69]: 2

 In [77]: set1.remove(2)   #当元素不存在时会报错
 ---------------------------------------------------------------------------
 KeyError                                  Traceback (most recent call last)
 77-0c37df1b7aa8> in ()
 ----> 1 set1.remove(2)

 KeyError: 2

In [71]: set1.discard(2)   #当元素不存在时不会报错

In [72]: set1
Out[72]: {4, 5, 6, 10, 20, 'e', 'h', 'l', 'o'}

In [73]: set1.discard(10)

In [74]: set1
Out[74]: {4, 5, 6, 20, 'e', 'h', 'l', 'o'}
2)集合的关系测试操作
• 交集: list_1.intersection(list_2)
• 并集: list_1.union(list_2)
• 差集: list_1.difference(list_2),list_2.difference(list_1)
• 对等差分 list_1.symmetric_difference(list_2)
• 子集 list_1.issubset(list_2)
• 父集 list_1.issuperset(list_2)
• 有无交集 list_1.isdisjoint(list_2)
##########################################
• 交集: list_1 & list_2
• 并集: list_1 | list_2
• 差集: list_1 - list_2,list_2 - list_1
• 对等差分list_1 ^ list_2

你可能感兴趣的:(python)