为啥今天又重提这个数据类型呢?平时用的少,等要用起来的时候才发现,自己对这块啥都不知道了,so,今天就把这块再梳理一下咯。

一、set集合,是一个无序且不重复的元素集合。这一点是非常重要的。

二、集合中的方法介绍:

  1. class set(object):
  2. """
  3. set() -> new empty set object
  4. set(iterable) -> new set object
  5.  
  6. Build an unordered collection of unique elements.
  7. """
  8. def add(self, *args, **kwargs): # real signature unknown
  9. """
  10. Add an element to a set,添加元素
  11.  
  12. This has no effect if the element is already present.
  13. """
  14. pass
  15.  
  16. def clear(self, *args, **kwargs): # real signature unknown
  17. """ Remove all elements from this set. 清除内容"""
  18. pass
  19.  
  20. def copy(self, *args, **kwargs): # real signature unknown
  21. """ Return a shallow copy of a set. 浅拷贝 """
  22. pass
  23.  
  24. def difference(self, *args, **kwargs): # real signature unknown
  25. """
  26. Return the difference of two or more sets as a new set. A中存在,B中不存在
  27.  
  28. (i.e. all elements that are in this set but not the others.)
  29. """
  30. pass
  31.  
  32. def difference_update(self, *args, **kwargs): # real signature unknown
  33. """ Remove all elements of another set from this set. 从当前集合中删除和B中相同的元素"""
  34. pass
  35.  
  36. def discard(self, *args, **kwargs): # real signature unknown
  37. """
  38. Remove an element from a set if it is a member.
  39.  
  40. If the element is not a member, do nothing. 移除指定元素,不存在不保错
  41. """
  42. pass
  43.  
  44. def intersection(self, *args, **kwargs): # real signature unknown
  45. """
  46. Return the intersection of two sets as a new set. 交集
  47.  
  48. (i.e. all elements that are in both sets.)
  49. """
  50. pass
  51.  
  52. def intersection_update(self, *args, **kwargs): # real signature unknown
  53. """ Update a set with the intersection of itself and another. 取交集并更更新到A中 """
  54. pass
  55.  
  56. def isdisjoint(self, *args, **kwargs): # real signature unknown
  57. """ Return True if two sets have a null intersection. 如果没有交集,返回True,否则返回False"""
  58. pass
  59.  
  60. def issubset(self, *args, **kwargs): # real signature unknown
  61. """ Report whether another set contains this set. 是否是子序列"""
  62. pass
  63.  
  64. def issuperset(self, *args, **kwargs): # real signature unknown
  65. """ Report whether this set contains another set. 是否是父序列"""
  66. pass
  67.  
  68. def pop(self, *args, **kwargs): # real signature unknown
  69. """
  70. Remove and return an arbitrary set element.
  71. Raises KeyError if the set is empty. 移除元素
  72. """
  73. pass
  74.  
  75. def remove(self, *args, **kwargs): # real signature unknown
  76. """
  77. Remove an element from a set; it must be a member.
  78.  
  79. If the element is not a member, raise a KeyError. 移除指定元素,不存在保错
  80. """
  81. pass
  82.  
  83. def symmetric_difference(self, *args, **kwargs): # real signature unknown
  84. """
  85. Return the symmetric difference of two sets as a new set. 对称差集
  86.  
  87. (i.e. all elements that are in exactly one of the sets.)
  88. """
  89. pass
  90.  
  91. def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
  92. """ Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中 """
  93. pass
  94.  
  95. def union(self, *args, **kwargs): # real signature unknown
  96. """
  97. Return the union of sets as a new set. 并集
  98.  
  99. (i.e. all elements that are in either set.)
  100. """
  101. pass
  102.  
  103. def update(self, *args, **kwargs): # real signature unknown
  104. """ Update a set with the union of itself and others. 更新 """
  105. pass

集合方法摘录

三、常用方法举例:

  1. linux = set(['alex','jack','rain','sb'])
  2. python = set(['sb','alex','mack','rachel'])
  3.  
  4. #集合linux与集合python 中都有的(交集)
  5. a = linux.intersection(python)
  6. print(a) # {'sb', 'alex'}
  7.  
  8. # 求差集 linux 中有而python中没有的
  9. b = linux.difference(python)
  10. print(b) # {'rain', 'jack'}

集合部分的简单操作也就这些内容了。

python 基础 set 集合类型补充的更多相关文章

  1. Python中的集合类型分类和集合类型操作符解析

    集合类型    数学上,把set称作由不同的元素组成的集合,集合(set)的成员通常被称作集合元素(set elements).    Python把这个概念引入到它的集合类型对象里.集合对象是一组无 ...

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

    python基础数据类型--集合(set) 集合是一个数学概念由一个或多个确定的元素所构成的整体叫做集合 集合中的三个特征 1.确定性(元素必须死可hash) 2.互异性(去重) 3.无序性(集合中的 ...

  3. python基础之序列类型的方法——字符串方法

    python基础之序列类型的方法--字符串方法 Hello大家好,我是python学习者小杨同学,经过一段时间的沉淀(其实是偷懒不想更新),我终于想起了自己的博客账号,所以这次带来的是序列方法的后半部 ...

  4. python基础之数值类型与序列类型

    Hello大家好,我是python学习者小杨同学,已经学习python有一段时间,今天将之前学习过的内容整理一番,在这与大家分享与交流,现在开始我们的python基础知识之旅吧. 数值类型与序列类型 ...

  5. python set type 集合类型的数据介绍 (set frozenset)

      python支持数学中的集合概念,如:通过in,not in 可以检查某元素是否在,不在集合中. python有两种集合类型,set(可以变的,不能哈希,不能用作字典的key),frozenset ...

  6. Python基础操作-集合

    在Python set是基本数据类型的一种集合类型,它有可变集合(set())和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法 ...

  7. Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之集合类型(set)

    集合!Python中的集合数据基本上是为了方便数学计算使用的. 什么是集合? 集合就是“确定的一堆东西”.集合里面的东西叫做元素. 特点:1. 集合里面是没有重复的元素的.           2. ...

  8. Python学习笔记——集合类型

    集合类型有两种不同的类型——可变集合(set)和不可变集合(frozenset) 可变集合不是可哈希的,不能用作字典的键,也不能用做其他集合中的元素 不可变集合是有哈希值的,能被用做字典的键或者是作为 ...

  9. python基础之集合,字符编码

    六.集合类型 1.用途:关系运算 2.定义方式:s = {1,2,’a’} {}内用,分隔开多个元素,每个元素都必须是不可变(即可hash)类型 强调:2.1集合内的元素时无序的 2.2集合内的元素不 ...

随机推荐

  1. java 实现登录验证码 (kaptcha 验证码组件)

    验证码的作用: 1.防止广告机注册和发帖.评论.2.防止暴力破解密码,特别是有管理员权限的密码. 在这里介绍一种非常实用的验证码生成工具:kaptcha 这个工具,可以生成各种样式的验证码,因为它是可 ...

  2. Linux 下 vim 编辑文件,解决中文乱码,设置Tab键空格数

    vim编辑文件的时候,输入中文就出现乱码 解决办法: 以哪个用户登录的就在哪个用户目录下创建文件 vimrc vim .vimrc       (.创建的是隐藏文件) 文件内容: set tabsto ...

  3. 杭电oj-1000-A+B

    Problem Description Calculate A + B. Input Each line will contain two integers A and B. Process to e ...

  4. c++函数常用

    isalnum 判断一个字符是否是字符类的数字或字母isalpha 判断一个字符是否是字母isblank 判断一个字符是否是空白字符(空格,水平制表符,TAB)iscntrl 判断一个控制符(ASCI ...

  5. Java集合框架(二)

    原文  http://www.jianshu.com/p/2070cb32accb List接口 查阅API,看 List 的介绍.有序的 collection (也称为序列).此接口的用户可以对列表 ...

  6. 记录解决python在spark运行加载第三方库的问题

    一般写python的我们经常会import一些常用的库,然后有时集群环境上的python没有这些库,怎么办呢? 通过一段时间的摸索发现有二种方式可以解决这个问题: 第一种方法: 下载对应python的 ...

  7. const 相关知识 const和指针、const和引用

    以前老是对const概念不清不楚,今天算是好好做个笔记总结一下.以下内容包括1)常量指针(指针本身是常量),2)指针常量(指针指向的是常量对象),3)常量引用,4)const成员函数. 常量指针,指针 ...

  8. Shell 读取用户输入

    14.2  读取用户输入 14.2.1  变量 上一章我们谈到如何定义或取消变量,变量可被设置为当前shell的局部变量,或是环境变量.如果您的shell脚本不需要调用其他脚本,其中的变量通常设置为脚 ...

  9. spring-boot 多模块化项目和EurekaServer的搭建

    Spring boot由于其 1.易于开发和维护.2.单个微服务启动快.3.局部修改部署容易.4.技术栈不受语言限制等优点受到越来越多公司的重视.spring-boot还集成了许多关于微服务开发的框架 ...

  10. 【windows】 配置一个本地的tomcat服务器

    配置tomcat 公司的许多业务都是用java+tomcat模式的,做本地测试的时候经常要搭建一个自己的tomcat服务器.整个操作不难,但是记录一下,万一以后遇到什么问题也可以放这里. ■ 安装ja ...