In order to remove the first element of the array, the index that must be considered is 0 as the index of the first element in any array is always 0. As like removing the last element from an array, removing the first element from the array can be processed using the same techniques.
让我们将这些技术应用于删除数组的第一个元素。现在,我们将逐个讨论用于从数组中删除第一个元素的方法和关键字。
The method pop() is used to delete the elements of the arrays, lists, etc in Python programming language. This mechanism works by using the index of the element which must be removed or deleted from the array.
所以,要删除数组的第一个元素,请考虑索引0。元素只是从数组中弹出并被删除。下面描述了“pop()”方法的语法。让我们使用这个方法删除数组的第一个元素。
arr.pop(0)
In this example, we are going to discuss about the process of removing the first element of an array by using pop() method. The steps followed to construct such program are as follows −
声明一个数组并在数组中定义一些元素。
通过使用pop()方法,在方法的括号内提及数组的第一个索引,即0,以删除第一个元素。
删除第一个元素后打印数组。
arr = [" Hello ", " Programming ", " Python ", " World ", " Delete ", " Element "] first_index = 0 print(" The elements of the array before deletion: ") print(arr) print(" The elements of the array after deletion: ") arr.pop(first_index) print(arr)
上述程序的输出如下:
The elements of the array before deletion: [' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ', ' Element '] The elements of the array after deletion: [' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']
The keyword del is used to delete the objects in Python. This keyword is also used in order to delete the last element of an array or any element by using its index. So, we use this keyword to delete specific objects or elements in Python. Following is the syntax of this keyword −
del arr[first_index]
在下面的示例中,我们将讨论使用“ del ”关键字删除数组的第一个元素的过程。
arr = [" Hello ", " Programming ", " Python ", " World ", " Delete ", " Element "] first_index = 0 print(" The elements of the array before deletion: ") print(arr) print(" The elements of the array after deletion: ") del arr[first_index] print(arr)
上述程序的输出如下:
The elements of the array before deletion: [' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ', ' Element '] The elements of the array after deletion: [' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']
The method delete() can remove the element from the array when its index is clearly mentioned. In order to use the method delete(), the array should be converted into the form of Numpy Array. The conversion of normal array to a numpy array can also be performed by using the module. The syntax of the delete() method is described below.
variable = n.delete(arr, first_index)
In this example, we are going to discuss about the process of removing the first element of an array by using delete() method of Numpy module.
import numpy as n arr = [" Hello ", " Programming ", " Python ", " World ", " Delete ", " Element "] variable = n.array(arr) first_index = 0 print(" The elements of the array before deletion: ") print(variable) variable = n.delete(arr, first_index) print(" The elements of the array after deletion: ") print(variable)
上述程序的输出如下:
The elements of the array before deletion: [' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ', ' Element '] The elements of the array after deletion: [' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']
我们可以清楚地观察到,这三个程序的输出是相同的,这告诉我们通过使用这三种方式,成功地从数组中删除了第一个元素。通过使用简单的技术,可以非常容易地删除数组中任意索引位置的元素。如果用户知道数组元素的索引,那么删除过程将变得非常简单。如果不知道索引,至少必须知道元素的值,以便可以应用“remove()”方法。
以上是Python程序删除数组中的第一个元素的详细内容。更多信息请关注PHP中文网其他相关文章!