集合的基本操作和基本运算

集合的基本操作和基本运算

  • 集合
    • 集合的创建
      • 创建一个空的集合
      • 多元素的集合创建
      • 集合的强转
    • 集合的基本操作
      • 改,不可以更改
    • 遍历
    • 比较
  • 集合的基本运算
    • 子集
    • 交集
    • 并集
    • 查集

集合

集合是无序的,不重复的且不可以更改的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的。以下是集合最重要的两点:

  • 去重,把一个列表变成集合,就自动去重了。
  • 关系测试,测试两组数据之前的交集、差集、并集等关系。

集合的创建

创建一个空的集合

这里需要注意,我们不可以直接用花括号"{}"

>>> st=set()
>>> st
set()
>>> st1={}
>>> type(st),type(st1)
(<class 'set'>, <class 'dict'>)
>>>

多元素的集合创建

>>> st={"a","b","c","d","e"}
>>> st,type(st)
({'a', 'd', 'b', 'c', 'e'}, <class 'set'>)
>>>

集合的强转

>>> li=[1,"a","n","university"]
>>> st_li=set(li)
>>> st_li,type(st_li)
({1, 'n', 'university', 'a'}, <class 'set'>)
>>>

>>> str="universitysity"
>>> st_str=set(str)
>>> st_str,type(st_str)
({'v', 'r', 'i', 'n', 'y', 'e', 's', 'u', 't'}, <class 'set'>)
>>>

>>> dic={"a":1,"b":2,"c":3,"d":4}
>>> st_dic=set(dic)
>>> st_dic,type(dic)
({'d', 'b', 'c', 'a'}, <class 'dict'>)
>>>

>>> tp=(1,2,3,4)
>>> st_tp=set(tp)
>>> st_tp,type(st_tp)
({1, 2, 3, 4}, <class 'set'>)
>>>

集合的基本操作

无序,不可查找

>>> st={1,2,"Hunan","Yingxiao","College"}
>>> st[2]
Traceback (most recent call last):
  File "", line 1, in <module>
TypeError: 'set' object does not support indexing
>>>

  • set.add()
  • set.update()
>>> st={1,2,"Hunan","Yingxiao","College"}
>>> st.add("Computer Science")
>>> st
{1, 2, 'Computer Science', 'Yingxiao', 'Hunan', 'College'}
>>> st.update("湖南","长沙")
>>> st
{1, 2, '长', 'Computer Science', 'Yingxiao', 'Hunan', '南', 'College', '沙', '湖'}
>>>

  • set.pop()
  • set.discard()
  • set.remove()
>>> st={1,2,"Hunan","Yingxiao","College"}
>>> st.pop()#删除排序最小的一个元素
1
>>> st
{2, 'Yingxiao', 'Hunan', 'College'}

>>> st={1,2,"Hunan","Yingxiao","College"}
>>> st.discard(2)#移除元素
>>> st
{1, 'Yingxiao', 'Hunan', 'College'}
>>> st.discard("Hunan")#移除元素
>>> st
{1, 'Yingxiao', 'College'}
>>> st.discard("YiYang")#如果集合中不包含此元素就不会做出任何操作
>>> st
{1, 'Yingxiao', 'College'}

>>> st={1,2,"Hunan","Yingxiao","College"}
>>> st.remove(2)
>>> st
{1, 'Yingxiao', 'Hunan', 'College'}
>>> st.remove("HUNAN")#如果此元素不被包含在内就会报错
Traceback (most recent call last):
  File "", line 1, in <module>
KeyError: 'HUNAN'
>>>
  • set.cleaer()
  • del.set名
>>> st={1,2,3}
>>> st.clear()
>>> st
set()
>>> st={1,2,3}
>>> del st
>>> st
Traceback (most recent call last):
  File "", line 1, in <module>
NameError: name 'st' is not defined
>>>

改,不可以更改

>>> st={1,2,"Hunan","Yingxiao","College"}
>>> st[1]="Hunan"
Traceback (most recent call last):
  File "", line 1, in <module>
TypeError: 'set' object does not support item assignment
>>>

遍历

>>> st={1,2,"Hunan","Yingxiao","College"}
>>> for i in st:
...     print(i,end=" ")
...
1 2 Yingxiao Hunan College >>>
>>>
>>> st={1,2,"Hunan","Yingxiao","College"}
>>> for index,value in enumerate(st):
...     print(index,value)
...
0 1
1 2
2 Yingxiao
3 Hunan
4 College
>>>

比较

集合的基本运算

子集

>>> a=set("abcd")
>>> b=set("cdef")
>>> c=set("ab")
>>> a,b,c
({'d', 'b', 'c', 'a'}, {'d', 'c', 'f', 'e'}, {'b', 'a'})
>>> c.issubset(a)
True
>>> c<a
True
>>> c<b
False
>>>

交集

>>> a=set("abcd")
>>> b=set("cdef")
>>> a.intersection(b)
{'d', 'c'}
>>> a&b
{'d', 'c'}
>>>

并集

>>> a=set("abcd")
>>> b=set("cdef")
>>> a.union(b)
{'a', 'd', 'b', 'c', 'e', 'f'}
>>> a|b
{'a', 'd', 'b', 'c', 'e', 'f'}
>>>

查集

>>> a=set("abcd")
>>> b=set("cdef")
>>> a.difference(b)
{'b', 'a'}
>>> a-b
{'b', 'a'}
>>> b-a
{'f', 'e'}
>>>

你可能感兴趣的:(集合的基本操作和基本运算)