如何在 Python 中从另一个列表中移除索引列表?
在本文中,我们将向您展示如何使用 Python 从原始列表中删除索引列表元素。现在我们看到完成此任务的两种方法:
使用 pop() 方法
使用 del 关键字
假设我们已经获取了一个包含一些元素的列表。我们将使用上面指定的不同方法从主列表中删除索引列表元素。
注意
我们必须以降序对索引列表进行排序,因为从开头删除元素会更改其他元素的索引,并且由于索引错位,删除另一个元素会导致错误的结果。
让我们用一个例子来演示这一点。
考虑以下列表:
list = ["Welcome," "to," "tutorialspoint," "python"]
indices List = [ 0, 2]
从开头删除元素的问题
如果我们从开头删除元素,则索引为 0 的元素“Welcome”将首先被删除。
现在修改后的列表如下所示:
list = ["to," "tutorialspoint," "python"]
如果我们删除索引为 2 的元素,即“python”,则修改后的列表如下所示
list = ["to," "tutorialspoint]
但结果应该是
list = ["to”,"python"]
因为索引为 0 和 2 的元素分别是“Welcome”和“tutorialspoint”。
因此,为了克服这个问题,我们从末尾删除元素,这样索引就不会受到影响。
方法 1:使用 pop() 函数
算法(步骤)
以下是执行所需任务的算法/步骤:
创建一个变量来存储输入列表
输入要删除项目的索引列表
使用sorted()函数(返回给定可迭代对象的排序列表。如果 reverse=True,则以降序排序,如果 reverse=False,则以升序排序。默认为 False,即升序),通过传递给定的索引列表和 reverse=True 作为参数,以降序对给定的索引列表进行排序。
使用for循环遍历给定的索引列表。
使用if条件语句检查相应的迭代器索引是否小于列表长度,并使用len()函数(len() 方法返回对象中的项目数)。
如果条件为真,则使用pop()函数删除对应索引处的项目。
删除给定索引处的项目后打印列表。
示例
以下程序在使用 pop() 函数基于索引列表删除主/原始列表的元素后返回列表:
# input list inputList = ["Welcome", 20, "to", "tutorialspoint", 30, "python"] # Entering the indices list at which the items are to be deleted givenIndices = [1, 4, 5] # Reversing Indices List indicesList = sorted(givenIndices, reverse=True) # Traversing in the indices list for indx in indicesList: # checking whether the corresponding iterator index is less than the list length if indx < len(inputList): # removing element by index using pop() function inputList.pop(indx) # printing the list after deleting items at the given indices print("List after deleting items at the given indices:\n", inputList)
输出
执行上述程序将生成以下输出:
List after deleting items at the given indices: ['Welcome', 'tutorialspoint']
我们创建了一个列表并向其中添加了一些随机值。然后我们创建了另一个索引列表来存储要删除的主列表元素的索引。为了避免在删除时发生冲突,我们以降序/递减顺序对索引列表进行了排序。之后,我们遍历索引列表,检查每个元素的索引是否小于主列表的长度,因为我们只能删除索引小于列表长度的元素。然后,通过传递索引,我们从主列表中删除该元素,并在从主列表中删除索引列表索引元素后显示最终的主列表。
方法 2:使用 del() 函数
算法(步骤)
以下是执行所需任务的算法/步骤:
使用sorted()函数通过传递给定的索引列表和 reverse=True 作为参数以降序对给定的索引列表进行排序。
使用for循环遍历给定的索引列表。
使用if条件语句检查相应的迭代器索引是否小于列表长度,并使用len()函数(len() 方法返回对象中的项目数)。
使用del关键字删除给定索引(迭代器)处的列表项。
删除索引列表的所有元素后打印列表。
语法
del list object [index]"
这是一个命令,可用于根据项目的索引位置从列表中删除项目。
如果列表为空或指定的索引超出范围,则 del 关键字将引发 IndexError。
示例
以下程序在使用 del 关键字基于索引列表删除主/原始列表的元素后返回列表:
inputList = ["Welcome", 20, "to", "tutorialspoint", 30, "python"] # index list givenIndices = [0, 3] # Reversing Indices List indicesList = sorted(givenIndices, reverse=True) # Traversing in the indices list for indx in indicesList: # checking whether the corresponding iterator index is less than the list length if indx < len(inputList): # removing element by index using del keyword del inputList[indx] # printing the list after deleting items at the given indices print("List after deleting items at the given indices:\n") print(inputList)
输出
List after deleting items at the given indices: [20, 'to', 30, 'python']
结论
在本文中,我们学习了如何使用 pop() 函数和 del 关键字从原始列表中删除索引列表。通过一个实际示例,我们还了解了为什么不能从开头删除元素。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP