集合 SET

太过爱你忘了你带给我的痛 2021-12-05 05:13 470阅读 0赞

集合 SET

定义:

只有键没有值的字典,保留无序的元素,没有索引。集合的元素不可变。

创建:

1.set_1 = set(seq)

2.set_2 = {}

  1. list_1 = [1,2,3,45,4,2]
  2. set_1 = set(list_1)
  3. set_2 = {1,1,2,3,4,3}
  4. print(set_1) #out:{1, 2, 3, 4, 45}
  5. print(set_2) #out:{1, 2, 3, 4}

创建只有一个元素的集合

  1. set_11 = set(('python',))
  2. set_12 = {
  3. 'python'}
  4. set_3 = set('hello')
  5. print(set_11) #out:{'python'}
  6. print(set_12) #out:{'python'}
  7. print(set_3) #out:{
  8. {'e', 'l', 'o', 'h'}

创建多个元素的集合

  1. set_21 = set(('hello','python'))
  2. set_22 = set(('hello','python'))
  3. set_23 = {
  4. 'hello','python'}
  5. print(set_21) #out:{
  6. {'hello', 'python'}
  7. print(set_22) #out:{
  8. {'hello', 'python'}
  9. print(set_23) #out:{
  10. {'hello', 'python'}

增:

单个增加

  1. set_a = set(('hello','python'))
  2. set_a.add('world')
  3. print(set_a) #out:{'hello', 'python', 'world'}

批量增加

  1. set_u = set(('hello',))
  2. set_p = set(('python',))
  3. set_u.update(set_p)
  4. print(set_u) #out:{'hello', 'python'}
  5. set_u.update({
  6. 'world'})
  7. print(set_u) #out:{'world', 'hello', 'python'}
  8. set_u.update('world')
  9. print(set_u) #{'w', 'python', 'world', 'o', 'l', 'd', 'r', 'hello'}

#

删:

remove:删除指定元素。若元素不存在则会报错。

discard:删除指定元素。若元素不存在则不会报错。

pop:随机删除元素,然而在交互模式,pop 是删除集合的第一个元素(排序后的集合的第一个元素)。

  1. set_r = set(('hello','python','world','qsl'))
  2. set_r.remove('hello')
  3. print(set_r) #out:{'world', 'qsl', 'python'}
  4. set_r.pop()
  5. print(set_r) #out:{'qsl', 'python'}
  6. set_r.discard('qsl')
  7. print(set_r) #out:{'python'}
  8. set_r.discard('hello')
  9. print(set_r) #out:{'python'}
  10. set_r.remove('hello') #out:KeyError: 'hello'

集合的操作:

  1. #并集
  2. #set_a|set_b = set_b|set_a = set_a.union(set_b) = set_b.union(set_a)
  3. set_a = set(('hello','python'))
  4. set_b = set(('hello','world'))
  5. print(set_a|set_b) #out:{'world', 'hello', 'python'}
  6. set_a.update(set_b)
  7. print(set_a) #out:{'world', 'hello', 'python'}
  8. print(set_a.update(set_b)) #out: None
  9. #交集
  10. #set_a.intersection(set_b)= set_a&set_b =set_b.intersection(set_a) = set_b&set_a
  11. set_a = set(('hello','python'))
  12. set_b = set(('hello','world'))
  13. print(set_a&set_b) #out:{'hello'}
  14. #差集:set_a中有而set_b中无
  15. #set_a.difference(set_b) = set_a-set_b
  16. set_a = set(('hello','python'))
  17. set_b = set(('hello','world'))
  18. print(set_a.difference(set_b)) #out:{'python'}
  19. print(set_a-set_b) #out:{'python'}
  20. #判断子集:查看set_a是否为set_b的子集
  21. # set_a.issubset(set_b) = set_a <= set_b
  22. set_a = set(('hello','python'))
  23. set_b = set(('hello','world'))
  24. print(set_a.issubset(set_b)) #out:False
  25. print(set_a <= set_b) #out:False

内置方法






























































































序号 方法 说明
1 add() 为集合添加元素,一次只能加一个
2 update() 给集合添加元素,一个可以加多个
3 copy() 拷贝一个集合
4 clear() 移除集合中的所有元素
5 discard() 删除集合中指定的元素,若不存在,不会报错
6 remove() 移除指定元素,若不存在,则会报错
7 pop() 随机移除元素
8 union()  返回两个集合的并集
9 intersection()  返回集合的交集
10 difference() 返回多个集合的差集
11 difference_update() 移除集合中的元素,该元素在指定的集合也存在
12

intersection_update()

 删除集合中的元素,该元素在指定的集合中不存在
13 issubset()  判断指定集合是否为该方法参数集合的子集。
14 isdisjoint()  判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False
15 issuperset()  判断该方法的参数集合是否为指定集合的子集
16 symmetric_difference()  返回两个集合中不重复的元素集合
17

symmetric_difference_update() 

移除当前集合中在另外一个指定集合相同的元素,并将另外一个指定集合中不同的元素插入到当前集合中

推导式:

和列表推导式相似,都是对一个序列进行操作。

但集合推导式的结果无序且不重复。

  1. list_1 = [1,2,3,-1,4,-2]
  2. set_1 = { i**2 for i in list_1 }
  3. print(set_1) #out:{16, 1, 4, 9}
  4. print(type(set_1)) #out:<class 'set'>

转载于:https://www.cnblogs.com/qianslup/p/11061679.html

发表评论

表情:
评论列表 (有 0 条评论,470人围观)

还没有评论,来说两句吧...

相关阅读

    相关 集合set

    35个问题测试你对Python集合的认识 如何通过掌握集合的基本原理来压制算法问题 图片来自Pexels的Andrea Piacquadio 在我追求掌握面试算法的过程中,我

    相关 set集合

    目录 1,set集合的特点: 1.1,set集合添加的数据不可重复 1.2 hashset无序 Treeset有序 1.2.1set集合的遍历方法 1.2.2Tree

    相关 Set集合

    Scala `Set`是相同类型成对的不同元素的集合。换句话说,一个集合是不包含重复元素的集合。 集合有两种:不可变(`immutable`)和可变(`mutable`)。可变

    相关 Set集合

    简介 > 无序,不可重复的集合 HashSet ①、HashSet:不能保证元素的顺序;不可重复;不是线程安全的;集合元素可以为 NULL; ②、对于 Has

    相关 集合 set

    集合 set :去重复,做操作 .add 是增加一个整体,如add('op')是加'op'.update 是增加一个一个的字符是加 o和p in ,no

    相关 set集合

    概述 Set接口继承Collection Set接口常用实现类 1. HashSet 实现了 Set 接口 “它不保证 set 的迭代顺序;特别是它不保证

    相关 集合Set

    集合的一个关键的特点就是不能存放重复的元素,二分搜索树是一个非常好的实现集合的底层数据结构 1、二分搜索树实现集合: ![在这里插入图片描述][watermark_ty