1 set 集合
2
3 集合的元素是唯一的,无序的。
4 里面的元素必须是可以hash
5 元素不可用索引
6 set可以迭代
7 ------------------------------------
8 能够索引的,list/str 元素可以重复
9 可变的,list/dict 元素/键值对 可以就地修改
10 不可变 str/int 不能进行原地修改
11 无索引序列的 dict 无序
12 ------------------------------------
13 集合类似字典: 可以用花括号定义,其中元素没有序列,而且元素不重复类似dict的键。
14 1、 创建set
15 >>>s1 = set("hello ,set")
16 >>>s1
17 {' ', ',', 'e', 'h', 'l', 'o', 's', 't'}
18 把字符串中的字符拆解开形成了集合。上面的字符串有2个e l ,但是在s1中只出现了一次,所以说set中是无重复的,而且无序
19 2、集合的函数:
20 1)x.add(elem)
21 --------------------------------------------------------------
22 Add an element to a set.增加一个元素到set中
23
24 This has no effect if the element is already present.
25 如果元素存在什么都不做
26 --------------------------------------------------------------
27 example:
28 m = [1,2,3]
29 x = set(m)
30 x.add(7)
31 print(x)
32 x.add(9)
33 print(x)
34
35 打印结果:
36 {1,2,3,7}
37 {1,2,3,9}
38 2)x.update(other) 将集合other并入源集合x中,x可以的是列表、元组、字典等,x可以是多个,逗号分割
39 ---------------------------------------------------------------
40 Update a set with the union of itself and others.
41 合并其他元素到set集合中
42 参数other必须是可迭代对象
43 就地修改
44 ----------------------------------------------------------------
45 example:
46 m = [1,2,3]
47 x = set(m)
48 other = [4,5,6]
49 x.update(other)
50 print(x,other)
51
52 打印结果:
53 {1, 2, 3, 4, 5, 6} [4, 5, 6]
54 ----------------------------------------------------------------
55 3)x.discard(other) 将other从集合x中移除,若元素不存在,不报异常
56 --------------------------------------------------------
57 Remove an element from a set if it is a member.
58 如果某个元素是成员,则从set中删除元素。
59 If the element is not a member, do nothing.
60 如果元素不存在,什么都不做
61 --------------------------------------------------------
62 example:
63 m = [1,2,3,4,5,6,32,2]
64 x = set(m)
65 x.discard(32)
66 x.discart(66)
67 print(x)
68
69 打印结果:
70 {1, 2, 3, 4, 5, 6}
71
72 4)x.remove(other) 将other从集合x中移除,若元素不存在,抛出KeyError
73 --------------------------------------------------------
74 Remove an element from a set; it must be a member.
75 从set中移除一个元素 它必须是成员
76 If the element is not a member, raise a KeyError.
77 元素不存在,抛出KeyError
78 --------------------------------------------------------
79 example:
80 m = [1,2,3,4,5,6,32,2]
81 x = set(m)
82 x.remove(2)
83 print(x)
84 x.remove(66)
85 print(x)
86
87 打印结果:
88 {32, 1, 3, 4, 5, 6}
89 ---------------------------------------------------------------------------
90 KeyError Traceback (most recent call last)
91 <ipython-input-6-cb927159bbb7> in <module>()
92 3 x.remove(2)
93 4 print(x)
94 ----> 5 x.remove(66)
95 6 print(x)
96
97 KeyError: 66
98
99 5)pop() 移除并返回任意的元素, 空集返回KeyError
100
101 --------------------------------------------------------
102 Remove and return an arbitrary set element.
103 移除元素并返回任意的元素
104 Raises KeyError if the set is empty.
105 空集返回KeyError
106 --------------------------------------------------------
107 example:
108 m = [1,2,3,4,5,6,32,2]
109 x = set(m)
110 x.pop()
111 print(x)
112 emptyset = set()
113 emptyset.pop()
114 print(emptyset)
115
116 打印结果:
117 {1, 2, 3, 4, 5, 6}
118 ---------------------------------------------------------------------------
119 KeyError Traceback (most recent call last)
120 <ipython-input-7-a8d46026713b> in <module>()
121 4 print(x)
122 5 emptyset = set()
123 ----> 6 emptyset.pop()
124 7 print(emptyset)
125
126 KeyError: 'pop from an empty set'
127
128 6)clear()移除所有元素
129
130 --------------------------------------------------------
131 Remove all elements from this set. 移除所有元素
132 --------------------------------------------------------
133 example:
134 m = [1,2,3,4,5,6,32,2]
135 x = set(m)
136 x.clear()
137 x
138
139 打印结果:
140 set()
141 --------------------------------------------------------------------------------
142 3、集合操作
143
144 并集:set.union(x) a|b
145 交集:set.intersection(s) a&b
146 差集:set.difference(s) a-b
147
148 1)元素和集合只有一种关系,要么属于某个集合,要么不属于
149 >>>x = set(['a','b','c','d'])
150 >>>'a' in x
151 True
152 >>>'e' in x
153 False
154 2)集合和集合
155
156 子集 :
157 有A和B 两个集合, 如果A的任何一个元素都是集合B的元素,则A是B的子集
158 如果A是B的一个子集,但在B中存在一个元素x不属于A,则称A是B的一个真子集
159 超集:
160 如果集合A中的每一个元素都在集合B中,且集合B中可能包含集合A中没有的元素,则
161 集合B是集合A的超集
162 a = set(['p','y','t','h','o','n'])
163 b = set(['p','y'])
164 b<a
165 True #b是a的子集
166 a.issuperset(b)#判断a是否是b的超集
167 True
168 ----------------------------------------
169 b = =set(['j','a','v','a'])
170 a<b
171 False #a不是b的子集
172 a.issuperset(b)
173 False #a不是b的超集
174
175 并集:
176 a = set(['p','y','t','h','o','n'])
177 b = set(['p','y','s'])
178 a|b #a和b的并集(a和b的所有元素,去除重复)
179 {'h', 'n', 'o', 'p', 's', 't', 'y'}
180 也可以:
181 a = set(['p','y','t','h','o','n'])
182 b = set(['p','y','s'])
183 a.union(b)
184 {'h', 'n', 'o', 'p', 's', 't', 'y'}
185
186 交集:
187 a = set(['p','y','t','h','o','n'])
188 b = set(['p','y','s'])
189 a&b #a和b的交集,取公共部分
190 {'p', 'y'}
191 也可以:
192 aintersection(b)
193 {'p','y'}
194
195 差集:
196 a = set(['p','y','t','h','o','n'])
197 b = set(['p','y','s'])
198 a-b #差集,取两者不同的元素
199 {'h', 'n', 'o', 't'}
200 也可以:
201 a.difference(b)
202
203 对称差集:
204 a.symmetric_difference(b)
205 a = set(['p','y','t','h','o','n'])
206 b = set(['p','y','s'])
207 a^b# 对称差集,反向交集 去掉交集输出
208 {'h', 'n', 'o', 's', 't'}
209 也可以:
210 a.symmetric_difference(b)