[Python入门及进阶笔记]Python-基础-集合小结

本文详细介绍了Python中的集合(set)及不可变集合(frozenset),涵盖了声明方式、常用操作如成员关系测试、增删元素等,以及集合间的数学运算如交集、并集、差集等,并提供了丰富的示例。

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

<style> <!-- h1, h2, h3, h4, h5, h6, p, blockquote {margin:0; padding:0} body {font-family:"Helvetica Neue",Helvetica,"Hiragino Sans GB",Arial,sans-serif; font-size:13px; line-height:18px; color:#737373; background-color:white; margin:10px 13px 10px 13px} table {margin:10px 0 15px 0; border-collapse:collapse} td, th {border:1px solid #ddd; padding:3px 10px} th {padding:5px 10px} a {color:#0069d6} a:hover {color:#0050a3; text-decoration:none} a img {border:none} p {margin-bottom:9px} h1, h2, h3, h4, h5, h6 {color:#404040; line-height:36px} h1 {margin-bottom:18px; font-size:30px} h2 {font-size:24px} h3 {font-size:18px} h4 {font-size:16px} h5 {font-size:14px} h6 {font-size:13px} hr {margin:0 0 19px; border:0; border-bottom:1px solid #ccc} blockquote {padding:13px 13px 21px 15px; margin-bottom:18px; font-family:georgia,serif; font-style:italic} blockquote:before {content:"\201C"; font-size:40px; margin-left:-10px; font-family:georgia,serif; color:#eee} blockquote p {font-size:14px; font-weight:300; line-height:18px; margin-bottom:0; font-style:italic} code, pre {font-family:Monaco,Andale Mono,Courier New,monospace} pre {display:block; padding:14px; margin:0 0 18px; line-height:16px; font-size:11px; border:1px solid #d9d9d9; white-space:pre-wrap; word-wrap:break-word} pre code {background-color:#fff; color:#737373; font-size:11px; padding:0} --> </style>

博客迁往:新地址(点击直达)

新博客使用markdown维护,线下有版本库,自己写的所以会定时更新同步,同时提供更好的导航和阅读体验

csdn对markdown支持不好,所以旧版不会花时间进行同步修订,抱歉


----------------------

集合

简介

python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素.

sets 支持 x in set, len(set), 和 for x in set。

集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.

作为一个无序的集合,sets 不记录元素位置或者插入点。因此,sets 不支持 indexing, slicing, 或其它类序列(sequence-like)的操作。

set为可变集合

frozenset为固定集合

可变集合特有的方法: add, remove, discard, pop, clear, 这些接受对象的方法, 参数必须是可哈希的

声明

用集合的工厂方法 set()和 frozenset():

set

>>> s = set('cheeseshop')  
>>> s  
set(['c', 'e', 'h', 'o', 'p', 's']) 

frozenset

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

Set 和 ImmutableSet

字符串->字符集

>>> set('hello')
set(['h', 'e', 'l', 'o'])

列表/元组->集合

>>> set([1,2,3,2,1])
set([1, 2, 3])
>>> set((1,2,3,2,1))
set([1, 2, 3])

甚至是 字典->集合

>>> a = {'name':'tom','age':22,'score':22}
>>> set(a)
set(['age', 'score', 'name'])

常用操作

成员关系
>>> h = set('hello')
>>> h
set(['h', 'e', 'l', 'o'])
>>> 'l' in h
True
>>> 'l' not in h
False
新增删除

新增单个元素s.add(x)

向 set “s”中增加元素 x

>>> a = set([1,2,3,4,2])
>>> a
set([1, 2, 3, 4])
>>> a.add(2)
>>> a
set([1, 2, 3, 4])
>>> a.add(5)
>>> a
set([1, 2, 3, 4, 5])

新增多个元素

s.update(t)

s |= t

>>> a = set([1,2,3])
>>> b = set([2,3,4])
>>> a.update(b)
>>> a
set([1, 2, 3, 4])
>>> b
set([2, 3, 4])

删除

s.remove(x)

从 set “s”中删除元素 x, 如果不存在则引发 KeyError

>>> a
set([1, 2, 3, 4, 5])
>>> a.remove(4)
>>> a
set([1, 2, 3, 5])
>>> a.remove(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 4

s.discard(x)

如果在 set “s”中存在元素 x, 则删除

>>> a
set([1, 2, 3, 5])
>>> a.discard(3)
>>> a
set([1, 2, 5])
>>> a.discard(3)
>>> a
set([1, 2, 5])

s.pop()

删除并且返回 set “s”中的一个不确定的元素, 如果为空则引发 KeyError

>>> a
set([1, 5])
>>> a.pop()
1
>>> a.pop()
5
>>> a.pop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'pop from an empty set'

s.clear()

删除 set “s”中的所有元素

>>> a
set([1, 2, 3, 4])
>>> a.clear()
>>> a
set([])

>>> b = set([1,2,3])
>>> del b
集合间操作

注意,集合操作可以通过函数进行,也存在等价的运算符

1.交集

s.union(t) 等价 s | t

返回一个新的 set 包含 s 和 t 中的每一个元素

2.并集

s.intersection(t) 等价 s & t

返回一个新的 set 包含 s 和 t 中的公共元素

3.差集

s.difference(t) 等价 s - t

返回一个新的 set 包含 s 中有但是 t 中没有的元素

4.差分集

s.symmetric_difference(t) 等价 s ^ t

返回一个新的 set 包含 s 和 t 中不重复的元素

>>> a = set([1,2,3])
>>> b = set([2,3,4])
>>> a.symmetric_difference(b)
set([1, 4])

5.关系判断

s.issubset(t) 等价 s <= t

测试是否 s 中的每一个元素都在 t 中

s.issuperset(t) 等价 s >= t

测试是否 t 中的每一个元素都在 s 中

6.浅拷贝

>>> a
set([1, 2, 3])
>>> b = a.copy()
>>> b
set([1, 2, 3])

其他

1.用的较少的函数

s.intersection_update(t) 等价 s &= t

返回只保留含有 set “t”中元素的 set “s”

s.difference_update(t) 等价 s -= t

返回删除了 set “t”中含有的元素后的 set “s”

s.symmetric_difference_update(t) 等价 s ^= t

返回含有 set “t”或者 set “s”中有而不是两者都有的元素的 set “s”


The end!

To be continue

wklken

Email: wklken@yeah.net

Blog: http://blog.youkuaiyun.com/wklken

2013-03-10

转载请注明出处,谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值