python学习笔记_集合的定义和常用方法
1、认识集合
定义:
s={1,2,3,4,5}
s=set("hello")
s=set(["steven","job","dave"])

用set和可迭代对象进行定义时,相当于执行了for循环
补充:集合属于可变类型,如果定义不可变类型的集合用frozenset(),
如s=frozenset("hello")
print(s) #结果为:frozenset({'h', 'o', 'l', 'e'})
特点:
不同元素组成
无序
集合中的元素必须是不可变 (字符串,数字,元祖)
2、常用方法
.add("a") #增加一个元素
.clear() #清空
.copy() #浅拷贝
.pop() #随机删
.remove("job") #指定删,删除元素不存在时会报错
.discard("job") #指定删,删除元素不存在时不会报错
3、基本运算
交集,并集,差集
a_s={"steve","job","dave","max"}
b_s={"job","dave","mark"}
print(a_s,b_s)
#求交集
print(a_s.intersection(b_s)) #写法1
print(a_s&b_s) #写法2
#求并集
print(a_s.union(b_s)) #写法1
print(a_s|b_s) #写法2
#求差集,相当于存在于左边,但不存在于右边的集合
print(a_s.difference(b_s)) #写法1
print(a_s-b_s) #写法2
print(b_s-a_s)
结果:
{'dave', 'steve', 'max', 'job'} {'dave', 'mark', 'job'}
{'dave', 'job'}
{'dave', 'job'}
{'job', 'dave', 'mark', 'steve', 'max'}
{'job', 'dave', 'mark', 'steve', 'max'}
{'steve', 'max'}
{'steve', 'max'}
{'mark'}
4、其他方法
a_s={"steve","job","dave","max"}
b_s={"job","dave","mark"}
c_s={"job","dave"}
#交叉补集 ,交集的补集,相当于并集减去交集的部分的集合
print(a_s.symmetric_difference(b_s)) #写法1
print(a_s^b_s) #写法2
#结果为:{'mark', 'max', 'steve'}
#.difference_update()
a_s.difference_update(b_s) # 相当于 a_s=a_s-b_s
print(a_s)
#结果为:{'max', 'steve'}
#.isdisjoint() 判断有没有交集,没有交集返回true
print(a_s.isdisjoint(b_s))
#结果为:false
#.issubset() 是否是某个集合的子集。相当于是s1<=s2
print(c_s.issubset(a_s)) #写法1
print(c_s<=a_s) #写法2
#.issuperset() 是否是某个集合的父集。相当于是s1>=s2
print(a_s.issuperset(c_s))
#结果为 true
#.update() 可以增加多个值。可以传元祖,列表等可迭代对象
a_s.update(b_s)
print(a_s)
b_s.update(("bob","zhou"))
print(b_s)
#结果为:
#{'steve', 'job', 'max', 'dave', 'mark'}
#{'bob', 'mark', 'job', 'dave', 'zhou'}
"""
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. (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. """
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. """
pass def isdisjoint(self, *args, **kwargs): # real signature unknown
""" Return True if two sets have a null intersection. """
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. """
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. """
passseset
set
python学习笔记_集合的定义和常用方法的更多相关文章
- Python学习笔记_我的参考网址
Python学习笔记, 下面记录网上搜到的可参考的网址: 一.关于Tkinter 1.Python3中tkinter模块使用方法详解 https://blog.csdn.net/Fighting_Bo ...
- python学习笔记整理——集合 set
python学习整理笔记--集合 set 集合的用途:成员测试和消除重复的条目,进行集合运算 注意:花括号或set()函数可以用于创建集合. 注意:若要创建一个空的集合你必须使用set(),不能用{} ...
- Python学习笔记-Day3-set集合操作
set集合,是一个无序且不重复的元素集合.定义方式类似字典使用{}创建 目前我们学过的数据类型: 1.字符串(str),2.整型(int),3.浮点型(float),4,列表(list) 5.元组(t ...
- python学习笔记(集合的使用)
集合 集合(set):把不同的元素组成一起形成集合,是python基本的数据类型. 集合元素(set elements):组成集合的成员 为什么需要集合? 集合的作用 1 .列表去重复数据 按照现有知 ...
- python学习笔记(7)——集合(set)
关键词#1.定义:无序不重复元素集, 基本功能包括关系测试和消除重复元素. 2.关键词:类似dict.只有key无value.常用于集合类数学运算. 3.创建 s=set() #入参可以是list.t ...
- 【Java学习笔记】<集合框架>定义功能去除ArrayList中的重复元素
import java.util.ArrayList; import java.util.Iterator; import cn.itcast.p1.bean.Person; public class ...
- Python学习笔记7—集合
set 拥有类似 dict 的特点:可以用{}花括号来定义:其中的元素没有序列,也就是是非序列类型的数据;而且,set 中的元素不可重复,这就类似 dict 的键. >>> s1 = ...
- 【Python学习笔记】集合
概述 集合的一般操作 内建函数进行标准操作集合 数学运算符进行标准操作集合 集合的应用 概述 python的集合(set)是无序不重复元素集,是一种容器.集合(set)中的元素必须是不可变对象,即可用 ...
- Python学习笔记 - function调用和定义
调用函数: #!/usr/bin/env python3 # -*- coding: utf-8 -*- # 函数调用 >>> abs(100) 100 >>> a ...
随机推荐
- 130-PHP子类通过类函数访问父类protected修饰的类成员
<?php class father{ //定义father类 //定义protected修饰的成员属性和方法 protected $money=1000000; protected funct ...
- 053-switch分支结构
<?php $week=3; //定义并初始化星期变量 switch($week){ case 0: //变量为0的情况 echo '星期日.'; break; case 1: //变量为1的情 ...
- Python LMDB的使用
在python中使用lmdb linux中,可以使用指令 pip install lmdb 安装lmdb包. ---- lmdb 数据库文件生成 增 改 删 查 1.生成一个空的lmdb数据库文件 # ...
- vmware搭建linux环境
参考博文:https://www.cnblogs.com/wuzm/p/10877990.html
- P 1015 德才论
转跳点:
- 大数据高可用集群环境安装与配置(08)——安装Ganglia监控集群
1. 安装依赖包和软件 在所有服务器上输入命令进行安装操作 yum install epel-release -y yum install ganglia-web ganglia-gmetad gan ...
- python运算表达式
运算符1.算术运算符:+,-,*,/,//(求整商),%,**(求多次方,左边为数,右边为多少次方)2.关系运算符:>,<,==,<=,>=,!=3.测试运算:in,not i ...
- mysql第四篇:数据操作之单表查询
单表查询 一.简单查询 -- 创建表 DROP TABLE IF EXISTS `person`; CREATE TABLE `person` ( `id` ) NOT NULL AUTO_INCRE ...
- Sequence Models Week 2 Emojify
Emojify! Welcome to the second assignment of Week 2. You are going to use word vector representation ...
- C++ 操作数据库类
#pragma once #include <string> #include <windows.h> #include <algorithm> #include ...