首页 > 编程语言 > 详细

Python 数据类型之 集合

时间:2019-04-14 00:43:05      阅读:98      评论:0      收藏:0      [点我收藏+]
Python 数据类型之 集合

无序且元素值唯一的组合

创建集合
    使用{} 可直接创建集合
        >>> Set_New = {1,2,3,4}
        >>> type(Set_New)
        <class ‘set‘>
        >>> Set_New
        {1, 2, 3, 4}
    使用set()将列表转换为集合
        >>> List_New = [1,2,3,4]
        >>> type(List_New)
        <class ‘list‘>
        >>> List_New
        [1, 2, 3, 4]
        >>> Set_New = set(List_New)
        >>> type(Set_New)
        <class ‘set‘>
        >>> Set_New
        {1, 2, 3, 4}

修改集合
    add() 接受单个任何数据类型的值,并将之添加到集合中
        >>> Set_New
        {1, 2, 3, 4}
        >>> Set_New.add(‘True‘)
        >>> Set_New
        {1, 2, 3, 4, ‘True‘}
        >>> Set_New(5,6)
    update() 接受集合和列表作为参数,并将其所有成员添加到集合中
        >>> Set_New
        {1, 2, 3, 4, ‘True‘}
        >>> Set_New.update({5,6,7},[7,8,9])
        >>> Set_New
        {1, 2, 3, 4, 5, 6, 7, 8, 9, ‘True‘}
        >>> Set_New.update({1,2,10})
        >>> Set_New
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ‘True‘}
        >>> Set_New.update([20,])
        >>> Set_New
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ‘True‘, 20}

删除集合元素
    discard() 方法接受一个单值作为参数,若值不存在,不会产生错去
        >>> Set_New
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ‘True‘, 20}
        >>> Set_New.discard(‘True‘)
        >>> Set_New
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20}
        >>> Set_New.discard(‘True‘)
        >>> Set_New
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20}
    remove() 方法接受一个单值作为参数,若值不存在,将引发KeyError例外
        >>> Set_New
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20}
        >>> Set_New.remove(10)
        >>> Set_New
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 20}
        >>> Set_New.remove(10)
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        KeyError: 10
    pop() 方法随机删除集合中某个元素的值,并返回删除值(在空集合中使用将引发KeyError列外)
        >>> Set_New
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 20}
        >>> Set_New.pop()
        2
    clear() 方法将清空集合中的所有值
        >>> Set_New = {1,2,3,4}
        >>> Set_New
        {1, 2, 3, 4}
        >>> Set_New.clear()
        >>> Set_New
        set()

集合间操作 (以下所有方法都是对称的)
    in 检测元素是否在集合中存在
        >>> a_set = {1,2,3,4,5,6}
        >>> 5 in a_set
        True
        >>> 9 in a_set
        False
    union() 方法返回一个新集合,里面有在两个集合中出现的所有元素  ==> 并集
        >>> b_set = {4,5,6,7,8,9}
        >>> a_set
        {1, 2, 3, 4, 5, 6}
        >>> a_set.union(b_set)
        {1, 2, 3, 4, 5, 6, 7, 8, 9}
    intersection()方法返回一个新集合,里面有在两个集合中同时出现的元素  ==> 交集
        >>> a_set
        {1, 2, 3, 4, 5, 6}
        >>> b_set
        {4, 5, 6, 7, 8, 9}
        >>> a_set.intersection(b_set)
        {4, 5, 6}
    difference() 方法返回一个新集合,里面有在a_set出现但未在b_set中出现的元素  ==> 求差
        >>> a_set
        {1, 2, 3, 4, 5, 6}
        >>> b_set
        {4, 5, 6, 7, 8, 9}
        >>> a_set.difference(b_set)
        {1, 2, 3}
    symmetric_difference()方法返回一个新集合,里面有只在一个集合中出现的元素
        >>> a_set
        {1, 2, 3, 4, 5, 6}
        >>> b_set
        {4, 5, 6, 7, 8, 9}
        >>> a_set.symmetric_difference(b_set)
        {1, 2, 3, 7, 8, 9}
子集和超集
        >>> a_set
        {1, 2, 3, 4, 5, 6}
        >>> b_set
        {4, 5, 6, 7, 8, 9}
        >>> b_set.update([1,2,3])
        >>> b_set
        {1, 2, 3, 4, 5, 6, 7, 8, 9}
        >>> a_set.issubset(b_set)
        True
        >>> b_set.issuperset(a_set)
        True
布尔上下文关系中的集合
    空集合为假,非空集合为真

Python 数据类型之 集合

原文:https://blog.51cto.com/sream/2377588

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!