{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 7.2 可变集合类型的操作"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"可变集合提供了一些关于元素更新、删除等相关操作的方法,常用操作及其方法描述如下表所示。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| 方法 | 描述 |\n",
"| :------------: | :------------------------------------------------------------ |\n",
"| s.add(x) | 在集合 s 中添加对象 x。 |\n",
"| s.remove(x) | 从集合 s 中删除对象 x;如果 x 不是集合 s 中的元素(x not in s),将引发<font color='red'>**KeyError 错误**</font> 。 |\n",
"| s.discard(x) | 如果 x 是集合 s 中的元素,从集合 s 中删除对象 x,如果s中不存在x也不会报错。 |\n",
"| s.pop() | 无参数,从集合中移除并返回任意一个元素, 如果集合为空则会引发 KeyError。 |\n",
"| s.clear() | 删除集合 s 中的所有元素。 |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 7.2.1 添加元素"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <font color='red'>**s.add(x)**</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"向集合 s 中**添加一个元素** x 的方法只有一个`s.add(x)`。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'n', 'p', 'h', 't', 'y', 'c++', 'o'}\n"
]
}
],
"source": [
"s = set('python')\n",
"\n",
"s.add('c++') # 字符串'1000'整体作为一个元素添加到集合中\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"images/ch7/4.png\" style=\"zoom:60%;\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 7.2.2 删除元素"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <font color='red'>1. **s.remove(x)**</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"从集合 s 中**删除一个指定元素** x,当要删除的元素 x 在集合 s 中**不存在时**,`s.remove(x)` <font color='red'>**会触发KeyError异常**</font>。 "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'n', 'h', 't', 'y', 'o'}\n"
]
}
],
"source": [
"s = set('python')\n",
"s.remove('p')\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"images/ch7/5.png\" style=\"zoom:60%;\">"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'A'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_175/745977858.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'python'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'A'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 集合s中不存在元素'A',将抛KeyError异常。\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyError\u001b[0m: 'A'"
]
}
],
"source": [
"s = set('python')\n",
"s.remove('A') # 集合s中不存在元素'A',将抛KeyError异常。\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"删除集合元素的remove方法在元素不存在时会引发KeyError错误,所以上面的代码中我们先通过成员运算判断元素是否在集合中。 \n",
"使用`s.remove(x)` 删除元素时,建议<font color='red'>**先做存在性测试**</font>,以避免触发异常导致程序无法正常结束。"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'n', 'p', 'h', 't', 'y', 'o'}\n"
]
}
],
"source": [
"s = set('python')\n",
"if 'A' in s:\n",
" s.remove('A') # 若集合中存在元素A,删除'A',避免异常\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <font color='red'>2. **s.discard(x)**</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"功能与`s.remove(x)`类似,也是从集合 s 中**删除一个指定元素** x。 \n",
"但当要删除的元素 x 在集合 s 中**不存在时**,`s.discard(x)`<font color='red'>**不会触发异常**</font>。"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'n', 'h', 't', 'y', 'o'}\n",
"{'n', 'h', 't', 'y', 'o'}\n"
]
}
],
"source": [
"s = set('python')\n",
"s.discard('p')\n",
"print(s)\n",
"s.discard('A') # 集合s中不存在元素'A',但不会抛异常,集合s没有任何变化\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <font color='red'>3. **s.pop()**</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pop方法可以从集合 s 中**随机删除一个元素**,该方法在删除元素的同时会获得被删除的元素,其<font color='red'>**返回值**</font>是被删除的元素。"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"n\n",
"{'p', 'h', 't', 'y', 'o'}\n"
]
}
],
"source": [
"s = set('python')\n",
"x = s.pop() # 从s中随机删除一个元素,返回被删除的元素赋值给x\n",
"\n",
"print(x)\n",
"print(s)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"images/ch7/6.png\" style=\"zoom:60%;\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"使用`s.pop()`方法时,如果<font color='red'>**集合为空**</font>则会触发<font color='red'>**KeyError**</font>。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s = set() # s是空集合\n",
"s.pop()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <font color='red'>4. **s.clear()**</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`s.clear()`方法可用于删除集合的所有元素,**清空集合**,只<font color='red'>**保留空集合对象**</font>。"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"set()\n"
]
}
],
"source": [
"city = {'北京', '上海', '广州', '深圳'}\n",
"city.clear()\n",
"print(city)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"images/ch7/7.png\" style=\"zoom:60%;\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <font color='red'>5. **del 命令**</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"del 命令可用于<font color='red'>**删除集合对象**</font>。 \n",
"与`s.clear()`的区别在于,del命令执行后,集合对象也会被删除,后续如果再调用该对象,将会抛NameError异常。"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'city' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_346/610532662.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mcity\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'北京'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'上海'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'广州'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'深圳'\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mcity\u001b[0m \u001b[0;31m# 集合对象city被删除\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcity\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# city对象已不存在,将抛NameError异常\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'city' is not defined"
]
}
],
"source": [
"city = {'北京', '上海', '广州', '深圳'}\n",
"del city # 集合对象city被删除\n",
"print(city) # city对象已不存在,将抛NameError异常"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}