Python_集合

本文全面解析Python中的集合数据类型,涵盖可变集合与不可变集合的特性、创建方式、增删改查操作、集合间的数学运算如交集、并集、差集等,以及集合与其他数据类型之间的转换方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.集合的概述

set是Python中一种基本数据类型,它分为可变集合(set)和不可变集合(frozenset)两 种。类似于其他语言,集合是一个无序不重复元素集,包括创建集合set、向集合中添加元素、删除集合中的元素、求集合的交集、并集、差集等操作。

可变集合: set集合类需要的参数必须是迭代器类型的,如:序列、字典等,然后转换成无序不重复的元素集。由于集合是不重复的,所以可以对字符串、列表、元组进行去重操作。

不可变集合: 集合中的元素不可以被改变,不能做插入和删除操作,其他方法和可变集合基本相同。

2.集合的创建.添加.删除

创建集合

>>> s = set()
>>> s
set()
>>> s = set([])
>>> s
set()
>>> s = set({})
>>> s
set()
>>> s = set('')
>>> s
set()


>>> str_s = set('createSet')
>>> print(str_s)
{'a', 'r', 'c', 't', 'S', 'e'}
>>> lst_s = set([1,2,3,1,2])
>>> print(lst_s)
{1, 2, 3}
>>> tuple_s = set((1,2,3,1,2))
>>> print(tuple_s)
{1, 2, 3}
>>> dic_s = set({1:2,3:4})
>>> print(dic_s)
{1, 3}
>>> set_s = set({1,2,3})
>>> print(set_s)
{1, 2, 3}

添加集合

add()方法是将要添加的元素作为一个整体添加到集合中,并去掉重复的

add(...)
 |      Add an element to a set.
 |      This has no effect if the element is already present.

 

update()方法是把要传入的元素拆分成单个字符,存于集合中,并去掉重复的

update(...)
 |      Update a set with the union of itself and others.

>>> s={1}
>>> s.add('one')
>>> s
{1, 'one'}
>>> s.update('two')
>>> s
{1, 'w', 'one', 't', 'o'}

 

删除集合

 pop(...)
 |      Remove and return an arbitrary set element.
 |      Raises KeyError if the set is empty.
 remove(...)
 |      Remove an element from a set; it must be a member.
 |      If the element is not a member, raise a KeyError.

 clear(...)
 |      Remove all elements from this set.

>>> s.remove(1)
>>> s
{'w', 'one', 't', 'o'}

>>> s.pop()
'one'
>>> s
{'t', 'o', 'w'}

>>> s.clear()
>>> s
set()

>>> del s
>>> s
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 's' is not defined

3.集合的遍历

>>> s = {1,'two','o'}
>>> for i in s:
...     print(i)
...
1
o
two

>>> s = set([1,2,'one'])
>>> for index,elem in enumerate(s):
...     print('索引:',index,'元素:',elem)
...
索引: 0 元素: 1
索引: 1 元素: 2
索引: 2 元素: one

enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串或集合)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

>>> s
{1, 2, 'one'}
>>> set(enumerate(s))
{(0, 1), (2, 'one'), (1, 2)}

>>> a=set({1:2,3:4}.values())
>>> a
{2, 4}

>>> a = set({1:2, 3:4}.keys())
>>> a
{1, 3}
>>> a=set({1:2,3:4}.items())
>>> a
{(1, 2), (3, 4)}

4.交集,并集,差集

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

#交集-->返回两个集合都包含的元素集合
>>> a&b
{'c', 'd'}
#并集-->返回两个集合所有的元素集合
>>> a|b
{'e', 'd', 'a', 'b', 'f', 'c'}

#返回不同时包含于两集合的元素集合
>>> a^b
{'e', 'a', 'b', 'f'}

#返回a包含,b不包含的元素
>>> a-b
{'a', 'b'}

#返回b包含,a不包含的元素
>>> b-a
{'e', 'f'}

>>> a = {'a', 'c', 'd', 'b'}
>>> b = {'a', 'b'}
#超集
>>> a.issuperset(b)
True
#子集
>>> b.issubset(a)
True

按照元素的内容作比较,判断是否有包含的关系
>>> a>b
True
>>> a==b
False
>>> a!=b
True

5.集合与其他类型的互转

#字符串与集合----字符串里重复的元素会被去掉,各元素的顺序被打乱

>>> s = 'this is a test'
>>> st = set(s)
>>> st
{'s', 'h', 'a', ' ', 't', 'i', 'e'}
>>> s = "".join(st)
>>> s
'sha tie'

#列表与集合---列表里重复的元素会被去掉

>>> lst = [1,2,3,1,2]
>>> st = set(lst)
>>> st
{1, 2, 3}
>>> lst = list(st)
>>> lst
[1, 2, 3]

#元组与集合--元组里重复的元素会被去掉

>>> tup = (1,2,3,1)
>>> st = set(tup)
>>> st
{1, 2, 3}
>>> tup = tuple(st)
>>> tup
(1, 2, 3)

6.不可变集合

>>> a=frozenset([1,2,3])
>>> a
frozenset({1, 2, 3})
>>> a.add(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'

7.集合的其他常用操作

setVar.discard(element)---查找元素并删除

 discard(...)
 |      Remove an element from a set if it is a member.
 |      If the element is not a member, do nothing.

>>> st = set([1,2,3])
>>> st.discard(1)
>>> st
{2, 3}
>>> print(len(st))
2

 

 difference(...)
 |      Return the difference of two or more sets as a new set.
 |      (i.e. all elements that are in this set but not the others.)

>>> s1 = {'a','b','c'}
>>> s2 = {'a','c','d'}
>>> s1.difference(s2)
{'b'}
>>> s2.difference(s1)
{'d'}

 

 union(...)  # 返回两集合的并集
 |      Return the union of sets as a new set.
 |      (i.e. all elements that are in either set.)

>>> s1 = {'a','b','c'}
>>> s2 = {'a','c','d'}
>>> s1.union(s2)
{'b', 'c', 'd', 'a'}
>>> s2.union(s1)
{'b', 'c', 'd', 'a'}
 

待处理:【 [ 'difference_update','intersection','intersection_update' 'isdisjoint','symmetric_difference','symmetric_difference_update' ]】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值