zoukankan      html  css  js  c++  java
  • python基本数据类型——set

    一、集合的定义

    set集合,是一个无序且不重复的元素集合。

    集合对象是一组无序排列的可哈希的值,集合成员可以做字典中的键。集合支持用in和not in操作符检查成员,由len()内建函数得到集合的基数(大小), 用 for 循环迭代集合的成员。但是因为集合本身是无序的,不可以为集合创建索引或执行切片(slice)操作,也没有键(keys)可用来获取集合中元素的值。

    二、集合的创建

    复制代码
    s = set()
    s = {11,22,33,44}
    *注:创建空集合时,只能用set(),如果用第二种方法s={},创建的实际上是一个空字典。
    s = {}
    print(type(s))
    <class 'dict'>
    
    a=set('boy')
    b=set(['y', 'b', 'o','o'])
    c=set({"k1":'v1','k2':'v2'})
    d={'k1','k2','k2'}
    e={('k1', 'k2','k2')}
    print(a,type(a))
    print(b,type(b))
    print(c,type(c))
    print(d,type(d))
    print(e,type(e))
    执行结果如下:
    {'o', 'b', 'y'} <class 'set'>
    {'o', 'b', 'y'} <class 'set'>
    {'k1', 'k2'} <class 'set'>
    {'k1', 'k2'} <class 'set'>
    {('k1', 'k2', 'k2')} <class 'set'>
    复制代码

    三、集合的功能

     源码

    基本功能:

    • 增加(add, update)
    复制代码
    a=set('python')
    a.add('tina')
    print(a)
    b=set('python')
    b.update('tina')
    print(b)
    执行结果如下:
    {'tina', 'o', 'p', 'n', 't', 'y', 'h'}
    {'o', 'i', 'p', 'a', 'n', 't', 'y', 'h'}
    ##################
    由以上代码可以看出,add是单个元素的添加,而update是批量的添加。输出结果是无序的,并非添加到尾部。
    复制代码
    • 删除(remove,discard,pop)
    复制代码
    c={'p', 'i', 'h', 'n', 'o', 'y', 't'}
    c.remove('p')
    print(c)
    
    c={'p', 'i', 'h', 'n', 'o', 'y', 't'}
    c.discard('p')
    print(c)
    c={'p', 'i', 'h', 'n', 'o', 'y', 't'}
    c.pop()
    print(c)
    执行结果如下: {'i', 'h', 't', 'o', 'y', 'n'} 
    #####
    
    当执行c.remove('p','i')和c.discard('p','i')时,报错:TypeError: remove() takes exactly one argument (2 given),说明remove和discard删除元素时都只能一个一个的删,同add对应。
    #################################################################################
    remove,pop和discard的区别:
    discard删除指定元素,当指定元素不存在时,不报错;
    remove删除指定元素,但当指定元素不存在时,报错:KeyError。
    pop删除任意元素,并可将移除的元素赋值给一个变量,不能指定元素移除。
    复制代码

    set的特有功能:

    复制代码
    s1 = {0}
    s2 = {i % 2 for i in range(10)}
    s = set('hi')
    t = set(['h', 'e', 'l', 'l', 'o'])
    print(s.intersection(t), s & t)  # 交集
    print(s.union(t), s | t)   # 并集
    print(s.difference(t), s - t)  # 差集
    print(s.symmetric_difference(t), s ^ t) # 对称差集
    print(s1.issubset(s2), s1 <= s2) # 子集(被包含)
    print(s1.issuperset(s2), s1 >= s2)   # 父集(包含)
    
    执行结果如下:
    {'h'} {'h'}
    {'i', 'e', 'h', 'l', 'o'} {'i', 'e', 'h', 'l', 'o'}
    {'i'} {'i'}
    {'e', 'l', 'o', 'i'} {'e', 'l', 'o', 'i'}
    True True
    False False
    
    s = {11,22,33}
    t = {22,44}
    print(s.isdisjoint(t))#(disjoint脱节的,)即如果没有交集,返回True,否则返回False
    s.difference_update(t)#将差集覆盖到源集合,即从当前集合中删除和B中相同的元素
    print(s)
    执行结果如下:
    False
    {33, 11}
    
    s = {11,22,33}
    t = {22,44}
    s.intersection_update(t)#将交集覆盖到源集合
    print(s)
    执行结果如下:
    {22}
    
    s = {11,22,33}
    t = {22,44}
    s.symmetric_difference_update(t)#将对称差集覆盖到源集合
    print(s)
    执行结果如下:
    {33, 11, 44}
    复制代码

    四、练习题

    寻找差异:哪些需要删除?哪些需要新建?哪些需要更新?

    复制代码
    #!/usr/bin/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},
    }
    new_set = set()
    old_set = set()
    for i in new_dict:
        new_set.add(i)
    for j in old_dict:
        old_set.add(j)
    new_add = new_set.difference(old_set)    #new_dict中有,old_dict中沒有
    old_del = old_set.difference(new_set)    #old_dict中有,new_dict中沒有
    update = new_set.intersection(old_set)    #old_dict和new_dict共同有的,需要把new_dict更新到old_dict中
    
    for k in new_add:
        old_dict[k] = new_dict[k]    #將new_dict中新增的內容添加到old_dict中
    for v in old_del:
        del old_dict[v]    #將old_dict中失效的內容刪除
    for m in update:
        old_dict[m] = new_dict[m]    #把new_dict更新到old_dict中
    
    print(old_dict)
    复制代码
  • 相关阅读:
    Git 总结
    .net报错大全
    对于堆和栈的理解
    html 局部打印
    c#面试问题总结
    算法题总结
    h5-plus.webview
    堆和栈,引用类型,值类型,指令,指针
    .NET framework具体解释
    前端之间的url 传值
  • 原文地址:https://www.cnblogs.com/yechanglv/p/6935633.html
Copyright © 2011-2022 走看看