|
1 | 1 | # 集合 |
2 | 2 |
|
| 3 | +集合(set)和字典(dict)类似,它是一组 key 的集合,但不存储 value。集合的特性就是:key 不能重复。 |
| 4 | + |
| 5 | +# 集合常用操作 |
| 6 | + |
| 7 | +## 创建集合 |
| 8 | + |
| 9 | +set 的创建可以使用 `{}` 也可以使用 set 函数: |
| 10 | + |
| 11 | +```python |
| 12 | +>>> s1 = {'a', 'b', 'c', 'a', 'd', 'b'} # 使用 {} |
| 13 | +>>> s1 |
| 14 | +set(['a', 'c', 'b', 'd']) |
| 15 | +>>> |
| 16 | +>>> s2 = set('helloworld') # 使用 set(),接收一个字符串 |
| 17 | +>>> s2 |
| 18 | +set(['e', 'd', 'h', 'l', 'o', 'r', 'w']) |
| 19 | +>>> |
| 20 | +>>> s3 = set(['.mp3', '.mp4', '.rmvb', '.mkv', '.mp3']) # 使用 set(),接收一个列表 |
| 21 | +>>> s3 |
| 22 | +set(['.mp3', '.mkv', '.rmvb', '.mp4']) |
| 23 | +``` |
| 24 | + |
| 25 | +## 遍历集合 |
| 26 | + |
| 27 | +```python |
| 28 | +>>> s = {'a', 'b', 'c', 'a', 'd', 'b'} |
| 29 | +>>> for e in s: |
| 30 | +... print e |
| 31 | +... |
| 32 | +a |
| 33 | +c |
| 34 | +b |
| 35 | +d |
| 36 | +``` |
| 37 | + |
| 38 | +## 添加元素 |
| 39 | + |
| 40 | +`add()` 方法可以将元素添加到 set 中,可以重复添加,但没有效果。 |
| 41 | + |
| 42 | +```python |
| 43 | +>>> s = {'a', 'b', 'c', 'a', 'd', 'b'} |
| 44 | +>>> s |
| 45 | +set(['a', 'c', 'b', 'd']) |
| 46 | +>>> s.add('e') |
| 47 | +>>> s |
| 48 | +set(['a', 'c', 'b', 'e', 'd']) |
| 49 | +>>> s.add('a') |
| 50 | +>>> s |
| 51 | +set(['a', 'c', 'b', 'e', 'd']) |
| 52 | +>>> s.add(4) |
| 53 | +>>> s |
| 54 | +set(['a', 'c', 'b', 4, 'd', 'e']) |
| 55 | +``` |
| 56 | + |
| 57 | +## 删除元素 |
| 58 | + |
| 59 | +`remove()` 方法可以删除集合中的元素。 |
| 60 | + |
| 61 | +看看例子: |
| 62 | + |
| 63 | +```python |
| 64 | +>>> s = {'a', 'b', 'c', 'a', 'd', 'b'} |
| 65 | +>>> s |
| 66 | +set(['a', 'c', 'b', 'd']) |
| 67 | +>>> s.remove('a') # 删除元素 'a' |
| 68 | +>>> s |
| 69 | +set(['c', 'b', 'd']) |
| 70 | +>>> s.remove('e') # 删除不存在的元素,会抛出 KeyErro |
| 71 | +Traceback (most recent call last): |
| 72 | + File "<stdin>", line 1, in <module> |
| 73 | +KeyError: 'e' |
| 74 | +``` |
| 75 | + |
| 76 | +## 交集/并集/差集 |
| 77 | + |
| 78 | +Python 中的集合也可以看成是数学意义上的无序和无重复元素的集合,因此,我们可以对两个集合作交集、并集等。 |
| 79 | + |
| 80 | +看看例子: |
| 81 | + |
| 82 | +```python |
| 83 | +>>> s1 = {1, 2, 3, 4, 5, 6} |
| 84 | +>>> s2 = {3, 6, 9, 10, 12} |
| 85 | +>>> s3 = {2, 3, 4} |
| 86 | +>>> s1 & s2 # 交集 |
| 87 | +set([3, 6]) |
| 88 | +>>> s1 | s2 # 并集 |
| 89 | +set([1, 2, 3, 4, 5, 6, 9, 10, 12]) |
| 90 | +>>> s1 - s2 # 差集 |
| 91 | +set([1, 2, 4, 5]) |
| 92 | +>>> s3.issubset(s1) # s3 是否是 s1 的子集 |
| 93 | +True |
| 94 | +>>> s3.issubset(s2) # s3 是否是 s2 的子集 |
| 95 | +False |
| 96 | +>>> s1.issuperset(s3) # s1 是否是 s3 的超集 |
| 97 | +True |
| 98 | +>>> s1.issuperset(s2) # s1 是否是 s2 的超集 |
| 99 | +False |
| 100 | +``` |
| 101 | + |
| 102 | +# 参考资料 |
| 103 | + |
| 104 | +- [使用dict和set - 廖雪峰的官方网站](http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868193482529754158abf734c00bba97c87f89a263b000) |
| 105 | +- [python字典和集合 | Alex's Blog](http://codingnow.cn/language/353.html) |
| 106 | +- [python - Why is it possible to replace set() with {}? - Stack Overflow](http://stackoverflow.com/questions/36674083/why-is-it-possible-to-replace-set-with) |
| 107 | + |
| 108 | + |
0 commit comments