python基础数据类型--集合(set)

集合是一个数学概念由一个或多个确定的元素所构成的整体叫做集合

集合中的三个特征

1.确定性(元素必须死可hash)

2.互异性(去重)

3.无序性(集合中的元素没有先后之分)如集合{1,2,3}和集合{2,3,1}算作一个集合

注意  集合存在的意义就是去重和关系运算

一、集合的创建

set1 = {1,2,3}

set2 = set({1,2,3})

单个元素的增加add(),     add的作用相当于列表中的append

序列的增加:update(),    update类似于extend方法,update方法可以支持同时传入多个参数

a = {1,2}
a.update([3,4],[1,2,7]) #迭代加 去重
>>>>a
{1, 2, 3, 4, 7}
a.update('hello')    #迭代加
>>>>a
{1, 2, 3, 4, 7, 'h', 'o', 'e', 'l'}

a.add('hello') #追加
>>>a
{1, 2, 3, 4, 7, 'h', 'o', 'hello', 'e', 'l'}

  

删除

集合删除单个元素有两种方法:

元素不在原集合里

set.discard(x)  不会抛出异常

set.remove(x)  会抛出keyError错误

a = {1,2,3,4}

a.discard(1)
print(a)
a.discard(1) #不会报错
print(a)
a.remove(1) #报错
print(a) >>>> {2, 3, 4}
{2, 3, 4}
Traceback (most recent call last):
File "D:/untitled/假期/2018-2-9/基础知识五--集合.py", line 19, in <module>
a.remove(1)
KeyError: 1

pop():由于集合是无序的,pop返回的结果不能确定,且当集合为空时调用pop会抛出KeyError错误,

clear(): 清空集合

a = {3,'a',2.1,1}
a.pop()
a.pop() #删除是无序的
a.clear() #清空
print(a)
a.pop() #报错
print(a) >>>>
set()
Traceback (most recent call last):
File "D:/untitled/假期/2018-2-9/基础知识五--集合.py", line 28, in <module>
a.pop()
KeyError: 'pop from an empty set'

查  

只能用for循环

for i in set1:
print(i)

集合的运算

# ①交集
set1 = {'a', 'b', 'c', 'd', '1', '4'}
set2 = {'1', '2', '3', '4', 'b', 'c'}
print(set1 & set2)
>>> {'c', '4', '1', 'b'}
print(set1.intersection(set2))
>>> {'c', '4', '1', 'b'} # ②反交集
set1 = {'a', 'b', 'c', 'd', '1', '4'}
set2 = {'1', '2', '3', '4', 'b', 'c'}
print(set1 ^ set2)
>>> {'d', '2', 'a', '3'}
print(set1.symmetric_difference(set2))
>>> {'d', '2', 'a', '3'} # ③并集
set1 = {'a', 'b', 'c', 'd', '1', '4'}
set2 = {'1', '2', '3', '4', 'b', 'c'}
print(set1 | set2)
>>> {'1', 'b', '2', '3', 'c', 'd', '4', 'a'}
print(set1.union(set2))
>>> {'1', 'b', '2', '3', 'c', 'd', '4', 'a'} # ④差集
set1 = {'a', 'b', 'c', 'd', '1', '4'}
set2 = {'1', '2', '3', '4', 'b', 'c'}
print(set1 - set2)
>>> {'d', 'a'}
print(set1.difference(set2))
>>> {'d', 'a'} print(set2 - set1)
>>> {'2', '3'}
print(set2.difference(set1))
>>> {'2', '3'} # ⑤子集与超集
set1 = {'A','B','C','a','b','c'}
set2 = {'A','B','C'}
print(set2 < set1)
>>> True
print(set2.issubset(set1))
>>> True print(set1 > set2)
>>> True
print(set1.issuperset(set2))
>>> True

  

不可变集合

# 不可变集合
set_frozen = frozenset({'A', 'B', 'C'})
print(set_frozen)
>>> frozenset({'C', 'B', 'A'})

最新文章

  1. Java后端实现图片压缩技术
  2. linux用终端上传文件和文件家到远程的服务器
  3. mybatis系列-15-查询缓存
  4. POJ 2774 Long Long Message (后缀数组模板)
  5. Android getActionBar()报空指针异常
  6. Android之日历触屏测试
  7. js template
  8. Makefile 入门与基本语法 分类: C/C++ ubuntu 2015-05-18 11:16 466人阅读 评论(0) 收藏
  9. 认识和理解css布局中的BFC
  10. HDU1061-Rightmost Digit(高速功率模)
  11. NodeJs中process.cwd()与__dirname的区别
  12. [PHP]PDO各方法在发生MYSQL断开时的反应
  13. ASP.NET MVC 路由篇二
  14. [tcpreplay] tcpreplay高级用法--使用tcpreplay-edit进行循环动态发包
  15. [Forward]Sweeping the IDisposable minefield
  16. 微信小程序插件使用
  17. go的基结构体如何使用派生结构体的方法
  18. ZOJ3513_Human or Pig
  19. C# 使用KingAOP面向切面编程
  20. fp-growth树创建代码及详细注释

热门文章

  1. codeblocks与MINGW的配置
  2. CentOS7编译安装httpd-2.4.41
  3. MPAndroidChart柱子上的文字的颜色dataSet.setValueTextColors
  4. Uber为何会成为共享经济中全球市值最大的独角兽企业?
  5. 「CF438D The Child and Sequence」
  6. 「CH6901」骑士放置
  7. 「HNOI2016」大数
  8. Acwing272 最长公共上升子序列
  9. MariaDB——备份与恢复
  10. IDEA中如何部署tomcat