数据类型总结:

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. wpf xmal基础

    1.名称空间的引用 比如想使用System.Windows.Controls名称空间 首先需要把改名称空间所在的程序集presentationFramework.dll引用到项目里 然后在根元素的起始 ...

  2. matlab读xls数据

    [ndata,label,abalone]=xlsread('data.xls') ndata:表示数字属性 label:表示类别属性 abalone:全部数据

  3. String to Double出现误差

    场景描述 做实际项目的时候,由于使用Double类的valueOf得到一个用String类型保存的金额参数(单位为元),当需要转换成以分为单位即整形表示(Integer类表示)时,需要用之前得到的do ...

  4. androidstudio下载地址

    google官网地址 https://developer.android.com/studio/index.html

  5. mysql 报错:java.lang.OutOfMemoryError: Java heap space

    原因:mysql会将查询到的记录全部发送到java端保存,而JVM中如果98%的时间是用于GC,且可用的Heap size 不足2%的时候将抛出此异常信息.JVM堆的设置是指java程序运行过程中JV ...

  6. PHP 分析1

    D:\wamp64\www\practice test 3: PHP 显示乱码 http://localhost/practice/ex1_5_stu.php <html><meta ...

  7. Math 对象 识记

    Math 对象用于执行数学任务. 1.使用 Math 的属性和方法的语法: var pi_value=Math.PI; var sqrt_value=Math.sqrt(15); 注释:Math 对象 ...

  8. [折腾纪实]JAVA的坑

    开贴记录使用JAVA踩的坑-- P.S. 学习编程最好的方法就是用一个贴心的IDE写,然后隔着屏幕都能感觉到IDE在骂自己SB-- Overridable method calls in constr ...

  9. php登录利用$token验证

    <?php $module = $_GET['module']; $action = $_GET['action']; $token = md5sum($module.date('Y-m-d', ...

  10. 关于微信端不支持window.location.reload()

    今天写了一个调查问卷页面,项目经理说要表单提交之后页面刷新,之间没沟通清楚,以为整个页面重载,所以刚开始就用了window.location.reload()的方法. 但是发现,在微信直接打开之后,居 ...