Python入门之set集合

set集合

set是一个无序且不重复的元素集合,有以下优点:

  • 1、访问速度快
  • 2、解决重复问题

1、set创建:

#字符串拆解形成set集合;其中字符串中重复字符,咋set集合中只会显示一个
>>> s1 = set("chengdd") #2个字符d
>>> s1
{'c', 'g', 'e', 'd', 'n', 'h'}    #只包含一个字符d
#list元素形成set集合,list如果有多个相同元素在集合中只显示一个;其中list元素可以为int/str
>>> s2 = set(["chengd","xrd",99,101,99])
>>> s2
{101, 99, 'chengd', 'xrd'}
#直接{}创建set集合
>>> s3 = {"chengd",999}
>>> s3
{'chengd', 999}

  注:

  • 在list元素创建set集合时,list中不能包list/dict元素;{}创建set集合时不能包含list/dict
    #list元素不能包含list/dict
    >>> s4 = set(["chengd","xrd",99,101,99,[1,2,3]])
    Traceback (most recent call last):
      File "<pyshell#14>", line 1, in <module>
        s4 = set(["chengd","xrd",99,101,99,[1,2,3]])
    TypeError: unhashable type: 'list'
    #{}创建元素不能包含list/dict
    >>> s5 = {1,2,"chengd",[1,2,3]}
    Traceback (most recent call last):
      File "<pyshell#15>", line 1, in <module>
        s5 = {1,2,"chengd",[1,2,3]}
    TypeError: unhashable type: 'list'
    >>> s6 = {1,2,"chengd",{"name":"xrd"}}
    Traceback (most recent call last):
      File "<pyshell#16>", line 1, in <module>
        s6 = {1,2,"chengd",{"name":"xrd"}}
    TypeError: unhashable type: 'dict'
  • 字典创建set集合时,集合元素为key
    >>> 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 },
    }
    >>> print(new_dict.keys())
    dict_keys(['#3', '#4', '#1'])
    >>> old_set = set(old_dict)
    >>> new_set = set(new_dict)
    >>> old_set
    {'#3', '#2', '#1'}
    >>> new_set
    {'#3', '#4', '#1'}

     

2、set常用方法:

  • set.add(value):添加单个元素;添加多个元素报错
    >>> s2
    {101, 99, 'chengd', 'xrd'}
    >>> s2.add("age")
    >>> s2
    {101, 99, 'chengd', 'xrd', 'age'}
    >>> s2.add("ages",123)
    Traceback (most recent call last):
      File "<pyshell#20>", line 1, in <module>
        s2.add("ages",123)
    TypeError: add() takes exactly one argument (2 given)

     

  • set.clear():清空集合元素

    >>> s2
    {101, 99, 'chengd', 'xrd', 'age'}
    >>> s2.clear()
    >>> s2
    set()
  • set.copy():浅拷贝set集合元素
    >>> s1
    {'c', 'g', 'e', 'd', 'n', 'h'}
    >>> s2 = s1.copy()
    >>> s2
    {'h', 'c', 'e', 'g', 'n', 'd'}
    #深浅拷贝和直接赋值区别:http://www.cnblogs.com/chengd/articles/7020282.html
  • set1.difference(set2):对比set1和set2,set1与set2不同的元素将会输出到成为新的set集合。(注意比较顺序)

    >>> s1 = {1,2,3,4,5}
    >>> s2 = {3,4,5,6,7,8}
    >>> s1
    {1, 2, 3, 4, 5}
    >>> s2
    {3, 4, 5, 6, 7, 8}
    >>> s1.difference(s2)
    {1, 2}    #s1在前面则表示s1包含但s2不包含元素
    >>> s2.difference(s1)
    {8, 6, 7}   #s2在前面则表示s2包含但s1不包含元素

    注:set1.difference(set2)的结果可以赋给新的set集合

    >>> s3 = s1.difference(s2)
    >>> s3
    {1, 2}
    >>> s4 = s2.difference(s1)
    >>> s4
    {8, 6, 7}
  • set1.difference_update(set2):对比set1和set2;set1和set2相同的选项将被去除,set1被改变,set2不变
    >>> s1
    {2, 3, 4}
    >>> s2
    {3, 4, 5, 6, 7, 8}
    >>> s1.difference_update(s2)
    >>> s1
    {2}
    >>> s2
    {3, 4, 5, 6, 7, 8}
  • set.discard():删除单个元素
    >>> s1
    {1, 2, 3, 4, 5}
    >>> s1.discard(1)
    >>> s1
    {2, 3, 4, 5}
    >>> s1.discard(5)
    >>> s1
    {2, 3, 4}
  • set1.intersection(set2):取set1和set2交集;结果可以赋给新的集合
    >>> s1
    {2, 3, 4}
    >>> s2
    {3, 4, 5, 6, 7, 8}
    >>> s1.intersection(s2)
    {3, 4}
    >>> s3 = s1.intersection(s2)
    >>> s3
    {3, 4}
  • set1.intersection_update(set2):取set1和set2交集;set1为交集结果
    >>> s1
    {1, 3, 5, 7}
    >>> s2
    {3, 4, 5, 6, 7, 8}
    >>> s1.intersection_update(s2)
    >>> s1
    {3, 5, 7}
    >>> s2
    {3, 4, 5, 6, 7, 8}
  • set1.isdisjoint(set2):判断set1和set2是否有交集,没有返回True
    #有交集返回Flase
    >>> s1
    {3, 5, 7}
    >>> s2
    {3, 4, 5, 6, 7, 8}
    >>> s1.isdisjoint(s2)
    False
    #没有交集返回True
    >>> s1 = {11,12,13}
    >>> s2
    {3, 4, 5, 6, 7, 8}
    >>> s1.isdisjoint(s2)
    True
  • set1.issuperset(s2):判断set1是否是set2父集,是返回真
    >>> s1 = {1,2,3,4,5}
    >>> s2 = {1,2,3}
    >>> s3 = {11,12,13}
    >>> s1.issuperset(s2)
    True
    >>> s1.issuperset(s3)
    False
  • set1.issubset():判断set1是否是set2子集,是返回真
    >>> s1 = {1,2,3}
    >>> s2 = {11,12,13}
    >>> s3 = {1,2,3,4,5,6}
    >>> s1.issubset(s3)
    True
    >>> s2.issubset(s3)
    False
  • set1.pop():从左到右删除元素,并输出结果;也可以直接赋值
    >>> s2
    {11, 12, 13}
    >>> s3
    {1, 2, 3, 4, 5, 6}
    >>> s3.pop()
    1
    >>> s3.pop()
    2
    >>> s3
    {3, 4, 5, 6}
    >>> i = s3.pop()    #赋值
    >>> i
    3
    >>> s2
    {11, 12, 13}
  • set.remove(value):删除指定元素
    >>> s = {1,2,3,4,5,6}
    >>> s.remove(1,3,4)    #一次不能删除多个元素
    Traceback (most recent call last):
      File "<pyshell#117>", line 1, in <module>
        s.remove(1,3,4)
    TypeError: remove() takes exactly one argument (3 given)
    >>> s.remove(14)    #删除不存在元素报错
    Traceback (most recent call last):
      File "<pyshell#118>", line 1, in <module>
        s.remove(14)
    KeyError: 14
    >>> s.remove(4)
    >>> s
    {1, 2, 3, 5, 6}
  • set1.symmetric_difference(set2):取set1和set2差集,可用于赋值新集合
    >>> s1
    {1, 2, 3}
    >>> s2
    {1, 2, 3, 4, 5}
    >>> s1.symmetric_difference(s2)
    {4, 5}
    >>> s2.symmetric_difference(s1)
    {4, 5}
  • set1.symmetric_difference_update(set2):取set1和set2差集,set1为差集结果
    >>> s1
    {1, 2, 3}
    >>> s2
    {1, 2, 3, 4, 5}
    >>> s1.symmetric_difference_update(s2)
    >>> s1
    {4, 5}
    >>> s2
    {1, 2, 3, 4, 5}
  • set1.union(set2):取set1和set2并集,结果可赋值新集合
    >>> s1
    {11, 12, 13}
    >>> s2
    {1, 2, 3, 4, 5}
    >>> s1.union(s2)
    {1, 2, 3, 4, 5, 11, 12, 13}
    >>> s1
    {11, 12, 13}
    >>> s2
    {1, 2, 3, 4, 5}
    >>> s3 = s1.union(s2)
    >>> s3
    {1, 2, 3, 4, 5, 11, 12, 13}
  • set1.update(set2):取set1和set2并集;set1为并集结果
    >>> s1
    {11, 12, 13}
    >>> s2
    {1, 2, 3, 4, 5}
    >>> s1.update(s2)
    >>> s1
    {1, 2, 3, 4, 5, 11, 12, 13}
    >>> s2
    {1, 2, 3, 4, 5}

     

3、常用内置函数

len(set):查看set元素个数

posted @ 2017-06-17 00:33  chengd  阅读(316)  评论(0)    收藏  举报