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学习笔记_集合的定义和常用方法的更多相关文章

  1. Python学习笔记_我的参考网址

    Python学习笔记, 下面记录网上搜到的可参考的网址: 一.关于Tkinter 1.Python3中tkinter模块使用方法详解 https://blog.csdn.net/Fighting_Bo ...

  2. python学习笔记整理——集合 set

    python学习整理笔记--集合 set 集合的用途:成员测试和消除重复的条目,进行集合运算 注意:花括号或set()函数可以用于创建集合. 注意:若要创建一个空的集合你必须使用set(),不能用{} ...

  3. Python学习笔记-Day3-set集合操作

    set集合,是一个无序且不重复的元素集合.定义方式类似字典使用{}创建 目前我们学过的数据类型: 1.字符串(str),2.整型(int),3.浮点型(float),4,列表(list) 5.元组(t ...

  4. python学习笔记(集合的使用)

    集合 集合(set):把不同的元素组成一起形成集合,是python基本的数据类型. 集合元素(set elements):组成集合的成员 为什么需要集合? 集合的作用 1 .列表去重复数据 按照现有知 ...

  5. python学习笔记(7)——集合(set)

    关键词#1.定义:无序不重复元素集, 基本功能包括关系测试和消除重复元素. 2.关键词:类似dict.只有key无value.常用于集合类数学运算. 3.创建 s=set() #入参可以是list.t ...

  6. 【Java学习笔记】<集合框架>定义功能去除ArrayList中的重复元素

    import java.util.ArrayList; import java.util.Iterator; import cn.itcast.p1.bean.Person; public class ...

  7. Python学习笔记7—集合

    set 拥有类似 dict 的特点:可以用{}花括号来定义:其中的元素没有序列,也就是是非序列类型的数据;而且,set 中的元素不可重复,这就类似 dict 的键. >>> s1 = ...

  8. 【Python学习笔记】集合

    概述 集合的一般操作 内建函数进行标准操作集合 数学运算符进行标准操作集合 集合的应用 概述 python的集合(set)是无序不重复元素集,是一种容器.集合(set)中的元素必须是不可变对象,即可用 ...

  9. Python学习笔记 - function调用和定义

    调用函数: #!/usr/bin/env python3 # -*- coding: utf-8 -*- # 函数调用 >>> abs(100) 100 >>> a ...

随机推荐

  1. 二十二、SAP中创建一个内表,并添加内容循环输出显示

    一.直接上代码 二.输出如下

  2. 《新标准C++程序设计》3.6-3.7(C++学习笔记9)

    一.成员对象和封闭类 (1)定义 一个类的成员变量如果是另一个类的对象,就称之为“成员对象”. 包含成员对象的类叫封闭类. (2)封闭类构造函数的初始化列表 在构造函数中添加初始化列表的写法: 类名: ...

  3. > 1> 2> &> /dev/null Linux重定向输出

    编译模拟器的 LINK 阶段产生了大量错误信息,定位不到第一行,所以将错误重定向到了一个文件: scons build/X86_VI_hammer_GPU/gem5.opt --default=X86 ...

  4. Essay写作没逻辑不要慌,掌握这几点即可

    今天文章的内容,真的是很多很多留学生的最大的问题,没有之一:逻辑.是的,你没有看错,也不用惊讶.大家的essay写作得分不高,很多时候不是因为语言问题.排除很多细节表达的不足,更让教授头疼的居然是:内 ...

  5. NiFi之Processor配置

    Processor(处理器)之配置 选择一个Processor,比如ExecuteSQL,从它的名字可以看出该处理器的功能就是去执行一个sql(当然是执行的该sql必须要有返回值的),下面就以Exec ...

  6. spring第9天(事务)

    依赖:spring-context,spring-jdbc(这个本身有依赖spring-tx,关于事务的),druid,mysql-connector-java,aspectjweaver五个 由于我 ...

  7. 关于SI4432数据手册的简单讲解

    SI4432是高度集成度单芯片无线ISM收发器件.EZRadioPRO系列包括了发射机.接收机和射频收发器,让设计工程师可以有选择的设计利用里面的无线部分. SI4432提供了先进的无线功能,包括连续 ...

  8. 16 ~ express ~ 添加博客分类

    一,创建表结构  /schemas/categories.js var mongoose = require('mongoose')   module.exports = new mongoose.S ...

  9. content编码

    1.content有5种属性 a.content:“”                     //为空 b.content:attr(TItle)     //可以获取当前选中标签的属性值 eg: ...

  10. 关于spring cloud “Finchley.RC2”版本在spring cloud config中的ArrayIndexOutOfBoundsException

    原文 https://www.cnblogs.com/Little-tree/p/9166382.html 在学spring cloud config的时候遇到一个ArrayIndexOutOfBou ...