{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 列表的删除"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"列表有3个方法可被用于删除列表中的元素,这三个方法分别为: \n",
"pop() \n",
"remove() \n",
"clear() \n",
"下面分别介绍其用法。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(1)pop()方法的使用方式为:\n",
"\n",
"<font color=Red>__ls.pop([i])__</font> \n",
"i 两边的方括号表示该参数是可选的,不是要求输入方括号 \n",
"其中ls为要操作的列表名,i为要删除的列表元素的序号。 \n",
"ls.pop(i)可用于移除列表中序号为“i”的一个元素,此处i为整数且不超过列表序号范围。 \n",
"当括号中无参数时,ls.pop()移除列表的最后一个元素。 \n",
"pop()方法是唯一一个能删除列表元素又能返回值的列表方法,其返回值为被移除的元素。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['7', '3', '1', '9', '8', '2', '6', '5', '4', '0']\n",
"['7', '3', '1', '9', '8', '2', '6', '5', '4']\n",
"['7', '3', '1', '9', '8', '2', '6', '5'] 4\n",
"['7', '3', '1', '8', '2', '6', '5']\n",
"['7', '3', '1', '8', '6', '5'] 2\n"
]
}
],
"source": [
"L = list('7319826540') # 将字符串转为列表L\n",
"print(L) # 输出列表元素['7', '3', '1', '9', '8', '2', '6', '5', '4', '0']\n",
"\n",
"L.pop() # 移除列表中最后一个元素\n",
"print(L) # 输出列表元素['7', '3', '1', '9', '8', '2', '6', '5', '4']\n",
"\n",
"s=L.pop() # 移除列表中最后一个元素,并将移除的元素赋值给s\n",
"print(L,s) # 输出列表元素及移除的数据['7', '3', '1', '9', '8', '2', '6', '5'] 4\n",
"\n",
"L.pop(3) # 移除列表中序号为3的元素\n",
"print(L) # 输出列表元素['7', '3', '1', '8', '2', '6', '5']\n",
"\n",
"s=L.pop(-3) # 移除列表中序号为3的元素,并将移除的元素赋值给s\n",
"print(L,s) # 输出列表元素及被移除的数据['7', '3', '1', '8', '6', '5'] 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(2)remove()方法的使用方式为:\n",
"\n",
"<font color=Red>__ls.remove(x)__</font>\n",
"\n",
"其中ls为要操作的列表名,x为要删除的数据。 \n",
"ls.remove(x)方法可用于删除列表中第一个与参数“x”值相同的元素。 \n",
"列表中存在多个与参数“x”值相同的元素时,只删除第一个,保留其它元素。 \n",
"当列表中不存在与参数“x”相同的元素时,抛出错误“ValueError: list.remove(x): x not in list”。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['7', '3', '1', '9', '8', '2', '6', '5', '4', '0']\n",
"['7', '3', '9', '8', '2', '6', '5', '4', '0']\n"
]
}
],
"source": [
"L = list('7319826540') # 将字符串转为列表L\n",
"print(L) # 输出列表元素['7', '3', '1', '9', '8', '2', '6', '5', '4', '0']\n",
"\n",
"L.remove(\"1\") # 删除列表中元素'1'(字符串)\n",
"print(L) # 输出修改过的列表['7', '3', '9', '8', '2', '6', '5', '4', '0']"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['7', '3', '9', '8', '2', '6', '5', '4', '0']\n"
]
}
],
"source": [
"L = list('7319826540') # 将字符串转为列表L\n",
"\n",
"L.remove(\"1\") # 删除列表中元素1(整数)\n",
"print(L) # 删除对象在列表中不存在,抛出错误ValueError: list.remove(x): x not in list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"为避免触发异常,在使用此方法删除元素前建议先做成员测试:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"L = list('7319826540') # 将字符串转为列表L\n",
"if 1 in L:\n",
" L.remove(1) # 若列表中存在整数1,则删除列表中元素1(整数)\n",
"print(L) # 查看结果['7', '3', '1', '9', '8', '2', '6', '5', '4', '0']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(3)clear()方法的使用方式为:\n",
"\n",
"<font color=Red>__ls. clear()__</font>\n",
"\n",
"ls. clear()方法可用于删除列表中全部元素,即清空列表。 \n",
"若L为当前操作的列表,则L.clear()作用与del L[:]相同。"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['7', '3', '1', '9', '8', '2', '6', '5', '4', '0']\n",
"[]\n"
]
}
],
"source": [
"L = list('7319826540') # 将字符串转为列表L\n",
"print(L) # 输出列表元素['7', '3', '1', '9', '8', '2', '6', '5', '4', '0']\n",
"L.clear() # 删除列表中全部元素\n",
"print(L) # 输出修改过的列表[]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[]\n"
]
}
],
"source": [
"L = list('7319826540') # 将字符串转为列表L\n",
"del L[:] # 删除列表中全部元素\n",
"print(L) # 输出修改过的列表[]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"L = list('7319826540') # 将字符串转为列表L\n",
"del L # 删除列表对象L\n",
"print(L) # NameError: name 'L' is not defined"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"当一个列表不再使用时,可以del 命令删除列表对象, del命令也可以被用于删除列表中的元素。"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"ls= list(range(5)) # 将range对象转为列表ls\n",
"print(ls) # 输出结果为:[0, 1, 2, 3, 4]\n",
"\n",
"del ls[1] # 索引,删除列表ls中序号为“1”的元素\n",
"print(ls) # 输出结果为:[0, 2, 3, 4]\n",
"\n",
"del ls[1:3] # 切片,删除列表对象ls中序号1,2的元素\n",
"print(ls) # [0, 4]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"ls= list(range(5)) # 将range对象转为列表ls\n",
"print(ls) # 输出结果为:[0, 1, 2, 3, 4]\n",
"\n",
"del ls # 删除列表对象ls\n",
"print(ls) # 列表ls不存在了,抛出错误, NameError: name 'ls' is not defined。"
]
},
{
"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
}