Python 如何通过索引从列表中删除元素
在本文中,我们将展示如何使用Python从列表中通过索引删除元素。这里我们用4种方法来完成这个任务:
- 使用del关键字从列表中删除元素
-
使用pop()函数从列表中删除元素
-
使用切片从列表中删除元素
-
使用索引从列表中删除多个元素
假设我们有一个包含一些元素的列表。我们将使用上述方法通过给定索引值来删除列表中的特定项目。
方法1:使用del关键字从列表中删除元素
以下是执行所需任务的算法/步骤:
步骤
- 创建一个变量来存储输入的列表。
-
输入要删除列表项的索引。
-
使用del关键字,删除给定索引处的列表项。
-
删除指定列表项后,打印列表。
语法
"del list object [index]"
这是一个命令,可以根据索引位置从列表中删除一个项目。
如果列表为空或指定的索引超出范围,del关键字将抛出IndexError。
示例
以下程序使用del关键字删除指定索引列表元素后返回列表-
# input list
inputList = ["Welcome", "to", "tutorialspoint", "python"]
# Enter the index at which the list item is to be deleted
givenIndex = 3
# deleting the list item at the given index using the del keyword
del inputList[givenIndex]
# printing the list after deleting a specified list item
print("List after deleting specified list item:", inputList)
输出
执行上述程序后,将会生成以下输出:
List after deleting specified list item: ['Welcome', 'to', 'tutorialspoint']
方法2:使用pop()函数从列表中删除元素
步骤
以下是执行所需任务的算法/步骤:
- 将给定的索引作为参数传递给 pop() 函数(这里pop()函数会删除列表中给定索引的元素)
-
删除指定的列表项后打印列表。
示例
以下程序使用pop()函数删除指定索引列表元素后返回列表:
inputList = ["Welcome", "to", "tutorialspoint", "python"]
givenIndex = 1
# Passing index to the pop function to remove that element of the list at that index
inputList.pop(givenIndex)
# printing the list after deleting an item at the entered index
print("List after deleting an item at the entered index:")
print(inputList)
输出
List after deleting an item at the entered index:
['Welcome', 'tutorialspoint', 'python']
方法3:使用切片从列表中删除元素
切片可以通过从原始输入列表中删除给定索引处的项来创建一个新列表。
将列表分为三个部分,以删除索引为N的元素。
- 范围从0到N-1的项
-
位于索引N位置的项
-
范围从N+1开始值一直延伸到列表的最后
步骤
以下是完成所需任务的算法/步骤:
- 创建一个变量来存储输入列表
-
使用 切片 将给定索引之前和之后的列表元素存储起来,也就是说它删除了给定索引处的项目。
-
打印删除指定列表项后的列表
示例
inputList = ["Welcome", "to", "tutorialspoint", "python"]
givenIndex = 3
# storing the list elements before the given index and after the given index
# using slicing which means it removes the item at the given index
inputList = inputList[:givenIndex] + inputList[givenIndex+1:]
# printing the list after deleting an item at the entered index
print("List after deleting an item at the entered index:\n", inputList)
输出
List after deleting an item at the entered index:
['Welcome', 'to', 'tutorialspoint']
方法4:从列表中删除多个索引元素
步骤
以下是执行所需任务的算法/步骤
- 创建一个变量来存储输入列表
-
输入要删除项目的索引列表
-
使用 sorted() 函数(返回给定可迭代对象的排序列表。如果reverse=True,则按降序排序;如果reverse=False,则按升序排序。默认为False,即升序)对给定的索引列表进行降序排序,通过传递给定的索引列表和reverse=True作为参数
-
使用 for循环,在给定的索引列表中进行遍历
-
使用 if条件语句,检查相应的迭代器索引是否小于列表长度,使用 len()函数(len()方法返回对象中的项目数)
-
通过使用 pop() 函数,如果条件为真,则移除相应索引处的项。
-
在给定索引处删除项后,打印列表。
示例
inputList = ["Welcome", "to", "tutorialspoint", "python"]
# Entering the indices list at which the items are to be deleted
givenIndices = [1, 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 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']
结论
在本文中,我们学习了4种不同的通过索引从列表中删除元素的方法。我们还学习了如何使用pop()和sorted()函数从列表中删除多个索引元素。