zoukankan      html  css  js  c++  java
  • Python 基础数据类型之set

    set是一个无序且不重复的元素集合,相当于字典的键,不重复,不可变

    一、set变量初始化

    A = set() #注意在创建空集合的时候只能使用s=set(),因为s={}创建的是空字典
    B = {"one", "two", "three", "four"}
    C = set('boy')
    D = set(['y', 'b', 'o'])
    E = set({"k1": 'v1', 'k2': 'v2'})
    F = {'k1', 'k2', 'k2'}
    G = {('k1', 'k2', 'k2')}
    print (A, type(A))
    print (B, type(B))
    print (C, type(C))
    print (D, type(D))
    print (E, type(E))
    print (F, type(F))
    print (G, type(G))
    #运行结果:
    (set([]), <type 'set'>)
    (set(['four', 'three', 'two', 'one']), <type 'set'>)
    (set(['y', 'b', 'o']), <type 'set'>)
    (set(['y', 'b', 'o']), <type 'set'>)
    (set(['k2', 'k1']), <type 'set'>)
    (set(['k2', 'k1']), <type 'set'>)
    (set([('k1', 'k2', 'k2')]), <type 'set'>)

    去除重复值

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    A = [1,3,2,3,4,3,4,5,6,3,23,4,3]
    B = ("yang", "yang", "yong", "ming", "ming")
    
    set1 = set(A)
    set2 = set(B)
    
    print set1
    print set2
    #运行结果
    set([1, 2, 3, 4, 5, 6, 23])
    set(['yang', 'yong', 'ming'])

    二、更新/删除

    set序列不能对元素进行修改,只可以增加和删除

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    A = set({1, 2, 3, 4, 5})
    B = set((3, 4, 5, 6, 7))
    
    A.add(7)  # 增加单个元素
    print ("add: %s" % A)
    A.remove(2)  # 移除单个元素
    print ("remove: %s" % A)
    A.discard(11)  # 存在则删除,不存在也不报错
    print ("discard: %s" % A)
    A.pop()  # 随机删除一个元素
    print ("pop: %s" % A)
    A.update(B) #更新一个可迭代的对象
    print ("update: %s" % A)
    A.clear() #清空set序列
    print ("clear: %s" % A)
    #运行结果
    add: set([1, 2, 3, 4, 5, 7])
    remove: set([1, 3, 4, 5, 7])
    discard: set([1, 3, 4, 5, 7])
    pop: set([3, 4, 5, 7])
    update: set([3, 4, 5, 6, 7])
    clear: set([])
    set([])

    三、集合操作

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    A = set({1, 2, 3, 4, 5})
    B = set((3, 4, 5, 6, 7))
    
    print A & B  # 交集,生成新序列
    print A | B  # 并集,生成新序列
    print A - B  # 差集(在a中,但不在b中),生成新序列
    print A ^ B  # 对称差集(项在a或b中,但不会同时出现在二者中),生成新序列
    
    print A.union(B)  # 并集,生成新序列
    print A.intersection(B)  # 交集,生成新序列
    print A.difference(B)  # 差集,生成新序列
    print A.symmetric_difference(B)  # 对称差集,生成新序列
    
    A.intersection_update(B)  # 交集,覆盖更新原序列
    A.difference_update(B)  # 差集覆盖更新原序列A
    A.symmetric_difference_update(B)  # 对称差集,覆盖更新原序列A
    
    print A.isdisjoint(B)  # 判断两个序列是不是不想交 bool
    print A.issubset(B)  # 判断B是否包含A bool,等同于A<=B
    print A.issuperset(B)  # 判断A是否包含B bool,等同于A>=B
    #运行结果
    set([3, 4, 5])
    set([1, 2, 3, 4, 5, 6, 7])
    set([1, 2])
    set([1, 2, 6, 7])
    
    set([1, 2, 3, 4, 5, 6, 7])
    set([3, 4, 5])
    set([1, 2])
    set([1, 2, 6, 7])
    
    False
    True
    True

    四、常用函数

        def add(self, element: _T)   # 添加一个元素
        def clear(self) # 清空集合
        def copy(self) # 浅复制
        def difference(self, *s: Iterable[Any]) #差集,生成新序列
        def difference_update(self, *s: Iterable[Any]) #差集,更新原序列
        def discard(self, element: _T) #  删除单个元素
        def intersection(self, *s: Iterable[Any]) # 交集,生成新序列
        def intersection_update(self, *s: Iterable[Any]) # 交集,更新原序列
        def isdisjoint(self, s: Iterable[Any]) # 判断两个集合是不是不相交
        def issubset(self, s: Iterable[Any]) # 判断集合是不是被其他集合包含,等同于a<=b
        def issuperset(self, s: Iterable[Any]) # 判断集合是不是包含其他集合,等同于a>=b
        def pop(self) # 弹出一个元素,随机的,不可指定
        def remove(self, element: _T) # 移除单个元素,如果该元素不存在在该集合,则会报错
        def symmetric_difference(self, s: Iterable[_T]) # 对称差集,生成新序列
        def symmetric_difference_update(self, s: Iterable[_T]) # 对称差集,更新原序列
        def update(self, *s: Iterable[_T]) # 更新单个元素,或其他集合
    作者:杨永明
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    Spring自动装配Bean
    Spring中Bean的作用域和生命周期
    Spring实例化Bean的三种方法
    Spring AOP详解
    Mybatis=====注解
    GBK和UTF-8文字编码的区别
    This Android SDK requires Android Developer Toolkit version 23.0.0 or above.
    问题:Unable to find a 'userdata.img' file for ABI armeabi to copy into the AVD folder.
    Ubuntu 系统下可以做什么?
    C语言结构体数组内带字符数组初始化和赋值
  • 原文地址:https://www.cnblogs.com/ming5218/p/7896868.html
Copyright © 2011-2022 走看看