数据类型总结:

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不同)

最新文章

  1. 【模块化编程】理解requireJS-实现一个简单的模块加载器
  2. Have Fun with Numbers及循环链表(约瑟夫问题)
  3. js拆分数组
  4. HTTP(socket)下载遇到valgrind提示的错误: Conditional jump or move depends on uninitialised value(s)
  5. JavaScript,Java,php的区分大小写问题
  6. 解决win7和ubuntu双系统ubuntu不能上网的问题
  7. mac openresty 源码安装 坑
  8. maven项目的配置
  9. Linux中对swap分区的配置
  10. JAVA记录-redis缓存机制介绍(二)
  11. nodejs typescript怎么发送get、post请求,如何获取网易云通信token
  12. JVM总结-Java 虚拟机是怎么识别目标方法(上)
  13. python3下安装Selenium插件和驱动
  14. R多线程并行计算
  15. ubuntu16.04下安装mysql详细步骤
  16. Tomcat的安装以及基本配置
  17. Tomcat源代码解析系列
  18. oc block 遍历数组及字典
  19. 数组队列C++实现
  20. cookie和session的介绍

热门文章

  1. MySQL之索引优化
  2. 三星首次更新Gear VR虚拟现实浏览器
  3. UNIX基础--安装应用程序: Packages 和 Ports
  4. linux 下执行.sh文件提示permission denied
  5. php干不了的活
  6. Linux安装mysql mysql5.5.40 &lt;NIOT&gt;
  7. 子字符查找KMP算法 - 子串自匹配索引表
  8. 线段树解决leetcode307. Range Sum Query - Mutable
  9. ZZNU 1993: cots&#39; friends
  10. 你会用::before、::after吗