搜索
首页后端开发Python教程Python程序删除数组中的第一个元素

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程序删除数组中的第一个元素的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
2小时的Python计划:一种现实的方法2小时的Python计划:一种现实的方法Apr 11, 2025 am 12:04 AM

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

Python:探索其主要应用程序Python:探索其主要应用程序Apr 10, 2025 am 09:41 AM

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

您可以在2小时内学到多少python?您可以在2小时内学到多少python?Apr 09, 2025 pm 04:33 PM

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础?如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础?Apr 02, 2025 am 07:18 AM

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到?如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到?Apr 02, 2025 am 07:15 AM

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

Python 3.6加载Pickle文件报错"__builtin__"模块未找到怎么办?Python 3.6加载Pickle文件报错"__builtin__"模块未找到怎么办?Apr 02, 2025 am 07:12 AM

Python3.6环境下加载Pickle文件报错:ModuleNotFoundError:Nomodulenamed...

如何提高jieba分词在景区评论分析中的准确性?如何提高jieba分词在景区评论分析中的准确性?Apr 02, 2025 am 07:09 AM

如何解决jieba分词在景区评论分析中的问题?当我们在进行景区评论分析时,往往会使用jieba分词工具来处理文�...

如何使用正则表达式匹配到第一个闭合标签就停止?如何使用正则表达式匹配到第一个闭合标签就停止?Apr 02, 2025 am 07:06 AM

如何使用正则表达式匹配到第一个闭合标签就停止?在处理HTML或其他标记语言时,常常需要使用正则表达式来�...

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器