Python程序删除数组中的第一个元素

2023年 8月 27日 92.1k 0

Python程序删除数组中的第一个元素

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.

让我们将这些技术应用于删除数组的第一个元素。现在,我们将逐个讨论用于从数组中删除第一个元素的方法和关键字。

使用pop()方法

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)

登录后复制

Example

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)

登录后复制

Output

上述程序的输出如下:

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 ']

登录后复制登录后复制登录后复制

Using the del Keyword

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]

登录后复制

Example

在下面的示例中,我们将讨论使用“ 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)

登录后复制

Output

上述程序的输出如下:

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 ']

登录后复制登录后复制登录后复制

Using Delete() Method of Numpy Module

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)

登录后复制

Example

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)

登录后复制

Output

上述程序的输出如下:

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程序删除数组中的第一个元素的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论