PYD7- 数据类型set、三元运算、函数

1、set

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. A中存在,B中不存在

        (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.  从当前集合中删除和B中相同的元素"""
        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.  取交集并更更新到A中 """
        pass

    def isdisjoint(self, *args, **kwargs): # real signature unknown
        """ Return True if two sets have a null intersection.  如果没有交集,返回True,否则返回False"""
        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. 对称差集,并更新到a中 """
        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

常用方法

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# se = {11,22,33,44}
# se.add(55)
# print(se)
# se.discard(66)
# #se.remove(66)
# print(se)
# bf = {21,22,23,25}
#
# #取se bf的交集
# ret1 = se.intersection(bf)
# #取交集并更新se
# se.intersection_update(bf)
#
# print(ret1)
# print(se)
#
# ret2 = se.issubset(bf)
# ret3 = se.issuperset(bf)
# print(ret2)
# print(ret3)
#
# bf.pop()
# print(bf)

se = {11,22,33,44}
be = {11,22,77,55}
r1 = se.difference(be)
r2 = be.difference(se)
print(r1)
print(r2)
ret = se.symmetric_difference(be)
print(ret)
# se.symmetric_difference_update(be)
# print(se)
ret = se.union(be)
print(ret)
print(se)
se.update([21])
print(se)

示例代码1

1.1习题:

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 }

}

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 }

}

#老字典key 相同的键值,将新字典key值更新到old,

#老字典中存在,新字典不存在的 将old中的值删除

目的:更新数据源

#!/usr/bin/env python
# -*- coding:utf-8 -*-
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 }
}
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 }
}
old_keys = old_dict.keys()
new_keys = new_dict.keys()
old_set = set(old_keys)
new_set = set (new_keys)
del_set = old_set.difference(new_set)
add_set = new_set.difference(old_set)
update_set = old_set.intersection(new_set)

部分代码

时间: 2024-08-05 11:12:37

PYD7- 数据类型set、三元运算、函数的相关文章

bytes数据类型,三元运算,进制互换

三元运算 如果这个条件成立就存这个值,如果那个条件成立就存那个值. 进制 bytes类型,字节数据类型也就是二进制类型,这个是python3专有数据类型,在python2里跟字符串是一个类型,也就是python2是不区分这个数据类型的. 比如说音频,视频文件都是二进制类型,也就是bytes类型.(python3通过socket在网络上传输数据时必须要用二进制格式,python2没有强制必须是二进制,字符串也可以) Python3中最大的新特性就是对文本和二进制数据做了更清晰的区分.文本通常是Un

三元运算 函数

1.三目运算 三元运算name = 'alex' if 1>1 else 'eric'print (name) 2.深浅拷贝copy deepcopy # str 一次性创建,不能被修改,只要修改,再创建# list 链表,下一个元素的位置,上一个元素的位置 str,数字:赋值/浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址, #拷贝,赋值:地址都一样import copyn1 = 123n2 = copy.copy(n1)  id(n1)id(n2) n2 = copy.deepcopy(

python-第04章-python的数据类型和三元运算

1.bytes数据类型 python 2 中字节和字符串都一样. python3 中,字节和字符串是两回事. 在Python3以后,字符串和bytes类型彻底分开了.字符串是以字符为单位进行处理的,bytes类型是以字节为单位处理的. 文本文件是个字符串,但是可以用二进制存放. 二进制转字符串————decode,反之用encode 先告诉编码,你是什么. 2.三元运算 result = 值1 if 条件 else 值2 如果 a>b,返回的是b = a,否则返回b = c 3.进制转换 二进制

Python基础入门(三)深浅拷贝、函数、内置函数、文件处理、三元运算、递归

深浅拷贝 import copy copy.copy() #浅拷贝 copy.deepcopy() #深拷贝 num = 110 copynum = num #赋值 一.数字和字符串 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. 1 import copy 2 #定义变量 数字.字符串 3 n1 = 123 4 #n1 = 'nick' 5 print(id(n1)) 6 7 #赋值 8 n2 = n1 9 print(id(n2)) 10 11 #浅

学习笔记1(三元运算、深浅拷贝、动态参数、全局变量与局部变量、set数据类型 )

(三元运算.深浅拷贝.动态参数.全局变量与局部变量.set数据类型 ) set 数据类型--无序,不重复的集合 一.三元运算.三木运算--减少代码量 name="X1"if 条件 else "x2" 例子:name =" alsx"  if 1==1 else "e" 二.深拷贝与浅拷贝:深拷贝拷贝所有的东西,浅拷贝拷贝最外面一层. 三.面向函数的编程(先前都是面向过程的编程) 生成函数>-执行函数>-返回执行的结

第三章、三元运算、文件处理、函数

第三章.三元运算.文件处理.函数 三元运算 三元运算又称三目运算,是对简单的条件语句的简写,如: 简单条件语句: if 条件成立: val = 1 else: val = 2 # 可以写成如下: val = 1 if 条件成立 else 2 文件处理 读,写,修改 读 f = open(file='d:/*.txt', mode='r', encoding='utf-8') date = f.read() f.close() r是只读模式,rb是以二进制读文件 第三方模块 chardet检测二进

004——Python 三元运算,Python函数

三元运算: if 1+1 == 2 : print (True) else: print (False) #等同于: print (True if 1+1==2 else False) 函数的基本语法def XX():     定义函数 #  return aa  返回值 # 或 pass      什么也不返回 # XX()   调用函数 #函数的有三中不同的参数: #------普通参数------ def func(name):   print (name) func('fanhaibo'

python之路(sed,函数,三元运算)

python之路(sed,函数,三元运算) 一.sed集合 1.set无序,不重复序列 2.创建 1 se = {11,22,33,33,44} 2 list() #只要是一个类加上()自动执行 list __init__ 3 list = [11,22] 4 5 s1 = set(list) 6 print(s1) 7 8 #创建集合 9 s1 = {11,22} 10 s2 = set('可循环的') 11 12 #操作集合 13 s = set() 14 print(s) 15 s.add

匿名函数、生成式、三元运算

匿名函数,关键字lambda,后接参数,然后是功能代码,整体返回结果 #匿名函数lambda fun = lambda x, y : x+y print(fun(2,3), type(fun)) 输出结果: 5 <class 'function'> 列表生成式,通过在[ ]中通过...for...in...快速生成数字列表 #列表生成式 list = [x for x in range(5)] print(list) 输出结果: [0, 1, 2, 3, 4] 生成器,通过在( )里面通过..

python函数 | 三元运算

三元运算符就是在赋值变量的时候,可以直接加判断,然后赋值 格式:[on_true] if [expression] else [on_false] 三元运算只适用于简单的if else判断,再多一层if判断就不适用了. 举例说明:比大小,大者返回 写一个函数 def max_min(a,b): if int(a) > int(b): return a else: return b print(max_min(1,3)) 三元运算 def max_min(a,b): z = a if a > b