数据类型总结:

1、按照数据可变不可变:

可变:列表、字典

不可变:数字、字符串、元组

x={'':1}
print(id(x))
x.update({'':2})
print(x)
print(id(x)) #执行结果
5584648
{'': 2, '': 1}
5584648
#列表亦同理
def a(x=[]):
x.append(1)
print(id(x))
return x
print(a())
print(a()) #执行结果
14045576
[1]
14045576
[1, 1]

2、按照访问方式:

直接访问:数字

顺序访问:列表、元组、字符串

映射:字典

3、按照存放元素个数:

容器类型:列表、元组、字典

原子:数字、字符串

bytes类型

定义:存8bit整数,数据基于网络传输或内存变量存储到硬盘时需要转成bytes类型,字符串前置b代表为bytes类型

集合:

特点:无序;元素必须是不可变类型数据;不同元素(无重复元素)

属于可变类

集合创建:

#创建可变集合
a = set() b=set('hello')
print(b)
# {'o', 'e', 'l', 'h'} #创建不可变集合
nb = frozenset(b)
print(nb)
frozenset({'l', 'e', 'h', 'o'})

常用方法:

##增

1、add  #一次只能添加一个集合元素

2、update  #增加可迭代数据,把每个元素迭代更新到集合,可以更新多个数据

##删

1、remove()  #删除的元素必须是集合中的元素,否则会报错

2、discard()  #删除的元素可以不是集合中的元素

3、pop()  #随机删除一个元素

4、clear()  #清空

##判断

1、isdisjoint()

判断两个集合是否有交集,没有则返回 True

2、issubset()

a.issubset(b) 判断a 是否是b的子集,是则返回 True

3、issuperset()

a.issuperset() 判断a是否是b的父集合,是则返回 True

##浅复制

1、copy()

##运算

1、交集 & intersection

a.intersection(b)  #a和b相同元素

2、并集 ^ union

a.union(b)  #a和b组成新的集合

3、差集 - difference

a.difference(b)  #存在a中,但不存在b中

4、symmetric_difference #对称差

a.symmetric_difference(b)== (a-b)^(b-a)

集合工厂函数:

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
"""
相当于s1-s2 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功能相同,删除元素不存在时不会抛出异常 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
"""
相当于s1&s2 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
"""
相当于s1<=s2 Report whether another set contains this set. """
pass def issuperset(self, *args, **kwargs): # real signature unknown
"""
相当于s1>=s2 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
"""
相当于s1^s2 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
"""
相当于s1|s2 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 查看 查看

set工厂函数

练习:

 # 数据库中原有
old_dict = {
"#1":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 },
"#2":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 }
"#3":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 }
} # cmdb 新汇报的数据
new_dict = {
"#1":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 800 },
"#3":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 }
"#4":{ 'hostname':c2, 'cpu_count': 2, 'mem_capicity': 80 }
} #添加:旧数据不存在而新数据有的 new_set-olb_set
#删除:新数据中没有,旧数据中存在的 old_set-new_set
#更新:旧数据存在,新数据也存在而且内容变更 olb_set&new_set(且value不同)

Python基础(四)-集合的更多相关文章

  1. Python 基础 四 面向对象杂谈

    Python 基础  四  面向对象杂谈 一.isinstance(obj,cls) 与issubcalss(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls ...

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

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

  3. Python基础(四) 基础拾遗、数据类型进阶

    一.基础拾遗 (一).变量作用域 外层变量,可以被内层变量直接调用:内层变量,无法被外层变量使用.这种说法在其它语言中适用,在python中除了栈以外,正常的变量作用域,只要执行声明并在内存中存在,该 ...

  4. 【笔记】Python基础四:迭代器和生成器

    一,迭代器协议和for循环工作机制 (一),迭代器协议 1,迭代器协议:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个stopiteration异常,以终止迭代(只能往 ...

  5. Python基础-week03 集合 , 文件操作 和 函数详解

    一.集合及其运算 1.集合的概念 集合是一个无序的,不重复的数据组合,它的主要作用如下 *去重,把一个列表变成集合,就自动去重了 *关系测试,测试两组数据之前的交集.并集.差集.子集.父级.对称差集, ...

  6. python 基础(四) 函数

    函数 一.什么是函数? 函数是可以实现一些特定功能的 小方法 或者是小程序 优点: 提高 了代码的后期维护 增加了代码的重复使用率 减少了代码量 提高了代码可读性 二.函数的定义 使用 def关键+函 ...

  7. Python基础四

    1. 集合 主要作用: 去重 关系测试, 交集\差集\并集\反向(对称)差集   2. 元组 只读列表,只有count, index 2 个方法 作用:如果一些数据不想被人修改, 可以存成元组,比如身 ...

  8. Python基础(三)——集合、有序 无序列表、函数、文件操作

    1.Set集合 class set(object): """ set() -> new empty set object set(iterable) -> n ...

  9. python 基础 set 集合类型补充

    为啥今天又重提这个数据类型呢?平时用的少,等要用起来的时候才发现,自己对这块啥都不知道了,so,今天就把这块再梳理一下咯. 一.set集合,是一个无序且不重复的元素集合.这一点是非常重要的. 二.集合 ...

  10. Python基础之集合

    一.定义: 二.基本操作: 三.运算: 交集&, 并集|, 补集-, 对称补集^, 子集<   超集> 四.集合推导式: 五.固定集合 frozenset 六.基本代码: # 1. ...

随机推荐

  1. 使用rsync无密码传输

    众多数据库服务器的管理过程中,在不同服务器间的文件传输是免不了的.您可以使用scp命令或FTP方法完成文件的发送和接收,这篇文章我将给大家介绍另外一种方法,这就是rsync命令.rsync是文件传输程 ...

  2. document对象相关的几个常用的方法

    方法  描述 var newP=createElement("p"); 创建了一个p标签,p也可替换为div,span等 var oldTxt=createTextNode(&qu ...

  3. java web开发环境搭建:jdk1.8+eclipse+tomcat8.0

    一.安装JDK 1.下载jdk-8-windows-i586.exe 2.双击安装(可以将安装路径选择到自己喜欢的地方) 3.配置环境变量 1.我的电脑->2.右键单击-> 然后将%JAV ...

  4. 导入导出Mysql数据库、表结构、表数据

    由sql文件导入 mysql -uusername -ppwd < ./abc.sql 导出整个数据库的表结构 mysqldump -uroot -pdbpasswd -d dbname > ...

  5. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network

    题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p   代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...

  6. Dreamweaver层使用八定律

    当然,这些并非真正的定律,而只是一些有益的忠告,使你免陷于使用层时可能的困顿中.原来有九条定律的,我们精简掉一条,还有下面的八条: 1. 如果你要嵌套层,决不要使用多重父层,应共享一个共同的单一父层. ...

  7. 最受欢迎的iOS第三方SDK

    http://www.raywenderlich.com/forums/viewtopic.php?t=4496

  8. LoadRunner日志(归档记录,以便自己查阅)

    1.当设置迭代次数大于1时,回放从第二次迭代开始发生错误 这种现象多是由于在"Run-time Setting"的"Browse Emulation"的设置中, ...

  9. ubuntu 设置静态IP之后不能上网。

    一个同事设置之后不能上网. 根据经验DNS有问题. 所以就设置8.8.8.8 之后就好了 DONE

  10. Python学习之旅--第二周--元组、字符串、运算、字典

    一.元组 另一种有序列表叫元组:tuple.tuple和list非常类似,但是tuple一旦初始化就不能修改,比如同样是列出同学的名字: # Author:Tim Gu tuple = (" ...