集合数据类型(set):集合是不重复的无需序列

1、集合数据类型的创建

a = {11,22,33}
#或
a = set() #创建空集合,不能用a={},这样创建的是字典类型

2、集合转换(将可迭代的转换为集合)

li = [11,22,33,11]
se = set(li)
print(se) #返回{33, 11, 22}

3、set方法总结se1 = {11,22}se2 = {22,33,44}se3 = {22,33}

se1.add(33)  #se1返回{33, 11, 22},增加元素
se1.clear() #se1返回set(),清空所有元素
se_new = se1.copy() #新集合se_new 返回{11, 22},浅拷贝 se_new = se1.difference(se2) #返回新集合,se1中存在但se2中不存在的元素
se1.difference_update(se2) #se1更新为se1中存在且se2中不存在的元素,se1={11} se_new = se1.symmetric_difference(se2) #对称不同,实际返回两个集合并集去掉两个集合中交集元素,se_new={33, 11, 44},
实际上是se1中有se2中没有 并集于 se2中有se1中没有的
se1.symmetric_difference_update(se2) #更新对称不同,se1更新为两个集合并集去掉两个集合中交集元素,se1={33, 11, 44},
实际上是se1中有se2中没有 并集于 se2中有se1中没有的
se_new = se1.intersection(se2) #返回一个新的集合,se_new为两个集合的交集,{22}
se1.intersection_update(se2) #更新se1为se1和se2的交集,se1={22}
se_new = se1.union(se2) #返回新集合,se1与se2的并集
se1.update(se2) #se1更新为,se1与se3的并集,se1={33, 11, 44, 22}
se1.discard(11) #se1={22},抛弃一个元素,如果参数中的元素不存在,不报错
element = se1.pop() #删除任意一个元素,并将删除的元素赋值为element,element=11,se1={22},若果是空集合则抛KeyError异常
se1.remove(11) #从se1中移除11元素,se1 = {22},如果参数在set中不存在这个元素则抛出KeyError异常
bol = se1.isdisjoint(se1) #判断se1与se2是否没有交集,如果没有返回True,如果有返回False,bol=False
bol = se3.issubset(se2) #判断se3是否是子集合,se3的元素完全包含在se2内,则返回True
bol = se2.issuperset(se3) #判断se2是否是父集合,se2的元素完全包含se3,则返回True

4、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. (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.
返回True 如果两个集合没有交集"""
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.
移除并返回任意集合元素
抛出KeyError 如果集合是空的
"""
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.
从集合中移除一个元素;它必须是一个成员。
如果这个元素不是一个成员,抛出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.)
作为一个新的集合返回两个集合的对称不同
(也就是所有元素正好在众集合中的一个)
实际上是a集合存在且b集合中不存在的元素,并集于,b集合存在且a集合不存在的元素
"""
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.
更新一个集合用自己和另一个集合的并集"""
pass def __and__(self, *args, **kwargs): # real signature unknown
""" Return self&value. """
pass def __contains__(self, y): # real signature unknown; restored from __doc__
""" x.__contains__(y) <==> y in x. """
pass def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass def __iand__(self, *args, **kwargs): # real signature unknown
""" Return self&=value. """
pass def __init__(self, seq=()): # known special case of set.__init__
"""
set() -> new empty set object
set(iterable) -> new set object Build an unordered collection of unique elements.
# (copied from class doc)
"""
pass def __ior__(self, *args, **kwargs): # real signature unknown
""" Return self|=value. """
pass def __isub__(self, *args, **kwargs): # real signature unknown
""" Return self-=value. """
pass def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass def __ixor__(self, *args, **kwargs): # real signature unknown
""" Return self^=value. """
pass def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass @staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass def __or__(self, *args, **kwargs): # real signature unknown
""" Return self|value. """
pass def __rand__(self, *args, **kwargs): # real signature unknown
""" Return value&self. """
pass def __reduce__(self, *args, **kwargs): # real signature unknown
""" Return state information for pickling. """
pass def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass def __ror__(self, *args, **kwargs): # real signature unknown
""" Return value|self. """
pass def __rsub__(self, *args, **kwargs): # real signature unknown
""" Return value-self. """
pass def __rxor__(self, *args, **kwargs): # real signature unknown
""" Return value^self. """
pass def __sizeof__(self): # real signature unknown; restored from __doc__
""" S.__sizeof__() -> size of S in memory, in bytes """
pass def __sub__(self, *args, **kwargs): # real signature unknown
""" Return self-value. """
pass def __xor__(self, *args, **kwargs): # real signature unknown
""" Return self^value. """
pass __hash__ = None

5、实例需求

old_dic = {"#1":11,"#2":22,"#3":33}
new_dic = {"#3":44,"#4":55,"#5":66}
需求:
1、old_dic与new_dic如果字典key值相同,则new的值更新到old里面
2、new里面存在old里面不存在,则在old里面添加
3、old中存在new中不存在则在old中删除
old_dic = {"#1":11,"#2":22,"#3":33}
new_dic = {"#3":44,"#4":55,"#5":66} old_set = set(old_dic.keys()) #key值转换为集合
new_set = set(new_dic.keys())
set_inter = old_set.intersection(new_set) #old与new交集
for i in set_inter: #更新old列表
old_dic[i] = new_dic[i]
set_new_dife = new_set.difference(old_set) #new里面存在,old里面不存在的集合
for i in set_new_dife:
old_dic[i] = new_dic[i]
set_old_dife = old_set.difference(new_set) #old里面存在,new里面不存在的集合
for i in set_old_dife:
old_dic.pop(i)
print(old_dic)

  


【python之路11】集合数据类型(set)的更多相关文章

  1. Python之路 day2 集合的基本操作

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa ''' #集合是无序的 集合的关系测试, 增加,删除,查找等操作 ''' #列表去重 ...

  2. Python入门笔记(11):集合

    一.目录 1.集合概述 2.关于集合的操作符.关系符号 3.集合的一系列操作(添加.更新.访问.删除) 4.关于集合的内建函数.内建方法 5.小结 二.集合概述 集合(set):把不同的元素组成一起形 ...

  3. 小白的python之路11/14

    视频69 固定命令的方式 1 vim /etc/profile 2 vim /etc/bashrc 3 vim /root/.bashrc 4 vim /root/.bash_profile 5 cd ...

  4. python之路——11

    王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 学习内容 一.装饰器 1.时间模块 time.time time.sleep 2.装饰器 原则---开放封闭 ...

  5. python之路---11 第一类对象 函数名 闭包 迭代器

    二十九. 1.函数名的运用    ①函数名是⼀个变量, 但它是⼀个特殊的变量, 与括号配合可以执⾏函数的变量 ②函数名是一个内存地址    ③ 函数名可以赋值给其他变量         ④函数名可以当 ...

  6. Python之路-字符编码&数据类型补充

    作业 三级菜单程序 menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{ }, '网易':{ }, 'google':{ } }, '中关村':{ '爱奇艺':{}, '汽车 ...

  7. 小白的python之路11/15 awk 77-78

    awk -F指定分隔符  eg下图指定 : 为分隔符,$1 $4 NF表示以分隔符为表准,该行分了几段    $NF表示取最后一段 正则表达式 表示打印第一个开头的用户名awk -F: '/nolog ...

  8. 小白的python之路11/3内存 进程 二进制软件包 rpm yum

    一 分区 查看swap分区 swapon -s mkswap /dev/sdb8 激活 swapon -a /dev/sdb8 swapon -s (sdb8进入了文件中) 提供内存服务 free ( ...

  9. 小白的python之路11/3总结

    ln-s 指定源是谁 l是软连接,其中源文件相当于快捷方式 1.打包 归档命令 tar -cvzf test.tar a.txt b.txt c.txt其中c是创建,v是详细信息,f是打包后文件名,a ...

随机推荐

  1. 如何让Spring MVC接收的参数可以转换为java对象

    场景: web.xml中增加了一个DispatcherServlet配置,并在同级目录下添加了**-servlert.xml文件,搭建起了一个spring mvc的restful访问接口. 问题描述: ...

  2. bash报错./mq.sh: line 15: warning: here-document at line 10 delimited by end-of-file (wanted `eof')

    [root@localhost tmp]# ./mq.sh./mq.sh: line 15: warning: here-document at line 10 delimited by end-of ...

  3. 解读QML之四

    解读QML之四 QML对象属性 每一个QML对象类型都定义了一系列属性.每创建一个该对象类型的实例,该实例的这些属性也自动被创建了.接下来我们讨论几种不同类型的属性. id属性 每一个QML对象类型都 ...

  4. 解决MyEclipse吃内存以及卡死的方法 (转)

    前言:MyEclipse5.5 大小 139M:MyEclipse6.5 大小 451M:MyEclipse7.0 大小 649M!下载服务器又是国外的...下载速度累人也就罢了,只要你工作性能一流. ...

  5. CentOS 修改DNS,固定IP等操作(网络)

    1.修改DNS 修改对应网卡的DNS的配置文件 vi /etc/resolv.conf 内容格式(西工大) nameserver 114.114.114.114 nameserver 202.117. ...

  6. 转:web_custom_request 函数

    语法:Int web_custom_request (const char *RequestName, <List of Attributes>, [EXTRARES, <List ...

  7. HDU 4421 ZOJ 3656 Bit Magic

    2-SAT,不要所有位置全部建好边再判断,那样会MLE的. 正解是,每一位建好边,就进行一次2-SAT. #include<cstdio> #include<cstring> ...

  8. UML学习小结

    最近在使用状态模式写一个仿Windows计算器的MFC程序,顺便学习了一下UML图的绘制,尤其是类图和状态图的绘制,这里做一下总结吧.    一.UML简介 统一建模语言UML(Unified Mod ...

  9. C#+QI的例子

    COM中,和我们打交道的是接口,也就是说类对我们是隐形的,那么我们要做开发,要使用这些功能,我们只能通过接口,通过接口暴露出来的方法,COM是一种服务器端/客户端架构,服务器端定义了操作的法,客户端通 ...

  10. BNU OJ 51005 BQG's Quadrilateral Bricks

    #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include ...