python 基础 set 集合类型补充
为啥今天又重提这个数据类型呢?平时用的少,等要用起来的时候才发现,自己对这块啥都不知道了,so,今天就把这块再梳理一下咯。
一、set集合,是一个无序且不重复的元素集合。这一点是非常重要的。
二、集合中的方法介绍:
class set(object):
"""
set() -> new empty set object
set(iterable) -> new set object Build an unordered collection of unique elements.
"""
def add(self, *args, **kwargs): # real signature unknown
"""
Add an element to a set,添加元素 This has no effect if the element is already present.
"""
pass def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from this set. 清除内容"""
pass def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of a set. 浅拷贝 """
pass def difference(self, *args, **kwargs): # real signature unknown
"""
Return the difference of two or more sets as a new set. A中存在,B中不存在 (i.e. all elements that are in this set but not the others.)
"""
pass def difference_update(self, *args, **kwargs): # real signature unknown
""" Remove all elements of another set from this set. 从当前集合中删除和B中相同的元素"""
pass def discard(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set if it is a member. If the element is not a member, do nothing. 移除指定元素,不存在不保错
"""
pass def intersection(self, *args, **kwargs): # real signature unknown
"""
Return the intersection of two sets as a new set. 交集 (i.e. all elements that are in both sets.)
"""
pass def intersection_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the intersection of itself and another. 取交集并更更新到A中 """
pass def isdisjoint(self, *args, **kwargs): # real signature unknown
""" Return True if two sets have a null intersection. 如果没有交集,返回True,否则返回False"""
pass def issubset(self, *args, **kwargs): # real signature unknown
""" Report whether another set contains this set. 是否是子序列"""
pass def issuperset(self, *args, **kwargs): # real signature unknown
""" Report whether this set contains another set. 是否是父序列"""
pass def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return an arbitrary set element.
Raises KeyError if the set is empty. 移除元素
"""
pass def remove(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError. 移除指定元素,不存在保错
"""
pass def symmetric_difference(self, *args, **kwargs): # real signature unknown
"""
Return the symmetric difference of two sets as a new set. 对称差集 (i.e. all elements that are in exactly one of the sets.)
"""
pass def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中 """
pass def union(self, *args, **kwargs): # real signature unknown
"""
Return the union of sets as a new set. 并集 (i.e. all elements that are in either set.)
"""
pass def update(self, *args, **kwargs): # real signature unknown
""" Update a set with the union of itself and others. 更新 """
pass
集合方法摘录
三、常用方法举例:
linux = set(['alex','jack','rain','sb'])
python = set(['sb','alex','mack','rachel']) #集合linux与集合python 中都有的(交集)
a = linux.intersection(python)
print(a) # {'sb', 'alex'} # 求差集 linux 中有而python中没有的
b = linux.difference(python)
print(b) # {'rain', 'jack'}
集合部分的简单操作也就这些内容了。
python 基础 set 集合类型补充的更多相关文章
- Python中的集合类型分类和集合类型操作符解析
		
集合类型 数学上,把set称作由不同的元素组成的集合,集合(set)的成员通常被称作集合元素(set elements). Python把这个概念引入到它的集合类型对象里.集合对象是一组无 ...
 - python基础数据类型--集合(set)
		
python基础数据类型--集合(set) 集合是一个数学概念由一个或多个确定的元素所构成的整体叫做集合 集合中的三个特征 1.确定性(元素必须死可hash) 2.互异性(去重) 3.无序性(集合中的 ...
 - python基础之序列类型的方法——字符串方法
		
python基础之序列类型的方法--字符串方法 Hello大家好,我是python学习者小杨同学,经过一段时间的沉淀(其实是偷懒不想更新),我终于想起了自己的博客账号,所以这次带来的是序列方法的后半部 ...
 - python基础之数值类型与序列类型
		
Hello大家好,我是python学习者小杨同学,已经学习python有一段时间,今天将之前学习过的内容整理一番,在这与大家分享与交流,现在开始我们的python基础知识之旅吧. 数值类型与序列类型 ...
 - python set type   集合类型的数据介绍 (set frozenset)
		
python支持数学中的集合概念,如:通过in,not in 可以检查某元素是否在,不在集合中. python有两种集合类型,set(可以变的,不能哈希,不能用作字典的key),frozenset ...
 - Python基础操作-集合
		
在Python set是基本数据类型的一种集合类型,它有可变集合(set())和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法 ...
 - Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之集合类型(set)
		
集合!Python中的集合数据基本上是为了方便数学计算使用的. 什么是集合? 集合就是“确定的一堆东西”.集合里面的东西叫做元素. 特点:1. 集合里面是没有重复的元素的. 2. ...
 - Python学习笔记——集合类型
		
集合类型有两种不同的类型——可变集合(set)和不可变集合(frozenset) 可变集合不是可哈希的,不能用作字典的键,也不能用做其他集合中的元素 不可变集合是有哈希值的,能被用做字典的键或者是作为 ...
 - python基础之集合,字符编码
		
六.集合类型 1.用途:关系运算 2.定义方式:s = {1,2,’a’} {}内用,分隔开多个元素,每个元素都必须是不可变(即可hash)类型 强调:2.1集合内的元素时无序的 2.2集合内的元素不 ...
 
随机推荐
- autobase之配置文件
			
配置文件内容: 1.db_info{},数据库链接属性,包括Oracle,dmdb,快立方 2.credit etc目录的路径 3.批量基础数据导入sql路径 4.用例执行日志存储目录路径 功能: 1 ...
 - @Controller 类中初始化问题解决办法
			
在Controller类中常常遇到有些参数需要初始化,甚至有些只允许初始化一次,而Controller类不像servelet类可以调用init()函数进行初始化,这里想到的办法是设置标记值,让初始化部 ...
 - nodejs中的require,exports使用说明
			
模块是一门语言编写大项目的基石,因此,了解如何组织.编写.编译.加载模块很重要.这里主要谈谈Node中的模块加载. 1.Node中的模块,主要使用require来加载模块,文件 require(&qu ...
 - react开发
			
webpack.config.js var webpack=require("webpack"); var htmlWebpackPlugin=require('html-webp ...
 - poj-1131-(大数)八进制转化成十进制
			
Description Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For e ...
 - Spring AOP: 织入的顺序
			
spring AOP 采用和 AspectJ 一样的优先顺序来织入增强处理:在进入连接点时,高优先级的增强处理将先被织入:在退出连接点时,高优先级的增强处理会后被织入. 当不同的切面里的两个增强处理需 ...
 - Redis set集合结构及命令详解
			
set 无序集合 集合的性质: 唯一性,无序性,确定性 注: 在string和link的命令中,可以通过range 来访问string中的某几个字符或某几个元素 但,因为集合的无序性,无法通过下标或范 ...
 - Redis进阶实践之十九  Redis如何使用lua脚本
			
一.引言 redis学了一段时间了,基本的东西都没问题了.从今天开始讲写一些redis和lua脚本的相关的东西,lua这个脚本是一个好东西,可以运行在任何平台上,也可以嵌入 ...
 - zabbix的各种键值
			
zabbix服务器端通过与zabbix agent通信来获取客户端服务器的数据,agent分为两个版本,在配置主机我们可以看到一个是agent,另一个是agent(active). agent:zab ...
 - python web开发-flask连接sqlite数据库
			
在之前的文章中我们介绍了如何在centOS中安装sqlite数据库. Sqlite安装完成后,本节就用flask来连接和操作sqlite数据库. 1. 数据准备 先在sqlite3中创建一 ...