集合

集合的特性

  不同元素组成

  无序

  集合中的元素必须时不可变类型

集合的方法

  add

 1 #赋值
 2 a = {1,2,3,4,5,6,7,"hello world"}
 3 
 4 #将子元素添加到集合a中
 5 a.add("add")
 6 
 7 #输出
 8 print (a)
 9 
10 
11 #结果
12 {1, 2, 3, 4, 5, 6, 7, 'add', 'hello world'}

 

  clear

#赋值
a = {1,2,3,4,5,6,7,"hello world"}

#清空集合
a.clear()

#输出
print (a)

#结果
set()

 

  copy

 1 #赋值
 2 a = {1,2,3,4,5,6,7,"hello world"}
 3 
 4 #拷贝
 5 b = a.copy()
 6 
 7 #输出
 8 print (b)
 9 
10 
11 #结果
12 {1, 2, 3, 4, 5, 6, 7, 'hello world'}

 

  difference

 1 #赋值
 2 a = {1,2,3,4,5,6,7,"hello world"}
 3 b = {1,2,3,4,5,'hello world'}
 4 
 5 #求a与b的差集(a - b 等效)
 6 c = a.difference(b)
 7 d = a - b
 8 
 9 #输出
10 print (c)
11 print (d)
12 
13 
14 #结果
15 {6, 7}
16 {6, 7}

 

  difference_update

 1 #赋值
 2 a = {1,2,3,4,5,6,7,"hello world"}
 3 b = {1,2,3,4,5,'hello world'}
 4 
 5 #求a与b的差集,更新到a中,没有返回值
 6 c = a.difference_update(b)
 7 
 8 #输出
 9 print (a)
10 print (c)
11 
12 
13 #结果
14 {6, 7}
15 None

 

  discard

#赋值
a = {1,2,3,4,5,6,7,"hello world"}
b = {1,2,3,4,5,'hello world'}

#删除集合中指定的元素,不返回值
c = a.discard('hello world')


#输出
print (a)
print (c)

#结果
{1, 2, 3, 4, 5, 6, 7}
None

 

  intersection

 1 #赋值
 2 a = {1,2,3,4,5,6,7,"hello world"}
 3 b = {1,2,3,4,5,'hello world'}
 4 
 5 #求交集
 6 c = a.intersection(b)
 7 d = a & b    #等效
 8 
 9 
10 #输出
11 print (c)
12 print (d)
13 
14 
15 #结果
16 {1, 2, 3, 4, 5, 'hello world'}
17 {1, 2, 3, 4, 5, 'hello world'}

 

  intersection_update

 1 #赋值
 2 a = {1,2,3,4,5,6,7,"hello world"}
 3 b = {1,2,3,4,5,'hello world'}
 4 
 5 #求交集,并更新到集合a,没有返回值
 6 c = a.intersection_update(b)
 7 
 8 
 9 #输出
10 print (a)
11 print (c)
12 
13 
14 #结果
15 {1, 2, 3, 4, 5, 'hello world'}
16 None

 

  isdisjoint

 1 #赋值
 2 a = {1,2,3,4,5,6,7,"hello world"}
 3 b = {1,2,3,4,5,'hello world'}
 4 c = {111,222}
 5 
 6 #如果没有交集返回ture
 7 d = a.isdisjoint(b)
 8 e = c.isdisjoint(b)
 9 
10 
11 #输出
12 print (d)
13 print (e)

 

  issubset

 1 #赋值
 2 a = {1,2,3,4,5,6,7,"hello world"}
 3 b = {1,2,3,4,5,'hello world'}
 4 c = {111,222}
 5 
 6 #是否子集
 7 d = b.issubset(a)
 8 e = b.issubset(c)
 9 
10 
11 #输出
12 print (d)
13 print (e)
14 
15 
16 #结果
17 True
18 False

 

  issuperset

#赋值
a = {1,2,3,4,5,6,7,"hello world"}
b = {1,2,3,4,5}
c = {111,222}

#是否父集
d = a.issuperset(b)
e = c.issuperset(b)


#输出
print (d)
print (e)

#结果
print (d)
print (e)

 

 

  pop

 1 #赋值
 2 a = {"hello",2,3,4,5,6,7,"hello world"}
 3 
 4 #随机删除一个元素,并返回
 5 b = a.pop()
 6 
 7 #输出
 8 print (b)
 9 
10 
11 #结果
12 hello

 

  remove

 1 #赋值
 2 a = {"hello",2,3,4,5,6,7,"hello world"}
 3 
 4 #指定删除一个元素,不返回值
 5 b = a.remove(4)
 6 
 7 #输出
 8 print (a)
 9 print (b)
10 
11 
12 #结果
13 {2, 3, 5, 6, 7, 'hello', 'hello world'}
14 None

 

  symmetric_difference

 1 #赋值
 2 a = {"hello",2,3,4,5,6,7,"hello world"}
 3 b = {'hello','hello world'}
 4 
 5 #交叉补集
 6 c = a.symmetric_difference(b)
 7 
 8 #输出
 9 print (c)
10 
11 #结果
12 {2, 3, 4, 5, 6, 7}

 

  symmetric_difference_update

 1 #赋值
 2 a = {"hello",2,3,4,5,6,7,"hello world"}
 3 b = {'hello','hello world'}
 4 
 5 #交叉补集,并跟新到集合a中
 6 a.symmetric_difference_update(b)
 7 
 8 #输出
 9 print (a)
10 
11 
12 #结果
13 {2, 3, 4, 5, 6, 7}

 

  union

 1 #赋值
 2 a = {1,2,3,4,5,6,7,}
 3 b = {'hello','hello world'}
 4 
 5 #求并集
 6 c = a.union(b)
 7 d = a ^ b     #等效
 8 
 9 #输出
10 print (c)
11 print (d)
12 
13 
14 
15 #结果
16 {1, 2, 3, 4, 5, 6, 7, 'hello world', 'hello'}
17 {1, 2, 3, 'hello world', 4, 5, 6, 7, 'hello'}

 

  update

 1 #赋值
 2 a = {1,2,3,4,5,6,7,}
 3 b = {'hello','hello world'}
 4 
 5 #将集合b(或者元素)更新到集合a中
 6 a.update(b)
 7 
 8 #结果
 9 print (a)
10 
11 
12 
13 #结果
14 {1, 2, 3, 4, 5, 6, 7, 'hello', 'hello world'}

 

  

  

posted on 2018-03-22 19:48  lurches  阅读(120)  评论(0编辑  收藏  举报

导航