搜索
首页后端开发Python教程使用内置函数的Python程序来反转数组的元素

使用内置函数的Python程序来反转数组的元素

Sep 06, 2023 pm 03:49 PM
内置函数python程序反转数组反转数组的

使用内置函数的Python程序来反转数组的元素

数组是一种数据结构,用于按顺序存储同质元素。存储的元素由索引值或键来标识。 Python 没有特定的数据结构来表示数组。但是,我们可以使用 List 数据结构或 Numpy 模块来处理数组。

在下面的文章中,我们将了解如何使用 python 内置函数反转数组的元素。反转数组元素的意思是把数组元素的顺序从前向后改变。

输入输出场景

现在让我们看一些输入输出场景,以了解数组元素的反转。

Input array:
[3, 5, 1, 4, 0, 2]
Output array:
[2, 0, 4, 1, 5, 3]

输入数组元素的顺序或排列被颠倒。

使用内置函数reverse()

通过使用Python内置的reverse()函数,我们可以反转数组的元素。以下是语法 -

reversed(seq) 

该函数将迭代器作为唯一参数,并返回一个反向迭代器。该函数返回一个list_reverse迭代器对象,因此我们需要使用list函数来获取反向列表。

示例

让我们举个例子,使用reverse()函数反转数组的元素。

# creating array
arr = [5, 2, 1, 6, 8]
print ("The original array is: ", arr) 
print() 

# Reverse the elements of the array
result = list(reversed(arr))

print("The array after reversing the elements is:", result)

输出

The original array is:  [5, 2, 1, 6, 8]
The array after reversing the elements is: [8, 6, 1, 2, 5]

reverse() 函数更改了数组元素的顺序。

使用 list.reverse() 函数

Python 中的 list.reverse() 用于反转列表对象的元素。以下是该函数的语法 -

list_obj.reverse() 

reverse() 方法不带任何参数,也不返回任何输出,而是更新原始列表对象。

示例

在此示例中,我们将使用 list.reverse() 函数。

# creating array
arr = [5, 2, 1, 6, 8]
print ("The original array is: ", arr) 
print() 

# Reverse the elements of the array
arr.reverse()
print("The array after reversing the elements is:", arr)

输出

The original array is:  [5, 2, 1, 6, 8]
The array after reversing the elements is: [8, 6, 1, 2, 5]

revers() 方法用反转的元素更新了给定的列表 arr。

使用 numpy.flip() 函数

我们可以使用numpy内置函数flip()来反转数组的元素。 numpy.flip() 函数返回一个具有相反数组元素的新 numpy 数组,并且它不会更改原始数组。以下是语法 -

numpy.flip(m, axis=None)

哪里,

  • m - 输入数组

  • axis - 它是一个可选参数,它接受一个整数或整数元组,默认情况下它是 None 。

示例

在此示例中,最初我们将定义一个 numpy 数组对象,然后使用 numpy.flip() 函数反转数组元素。

import numpy as np

# creating array
arr = np.array([9, 3, 2, 1, 6, 8, 5])
print("The original array is: ", arr) 
print() 

# Reverse the elements of the array
result = np.flip(arr)
print("The array after reversing the elements is:", result)

输出

The original array is:  [9 3 2 1 6 8 5]
The array after reversing the elements is: [5 8 6 1 2 3 9]

我们使用 numpy 内置函数 Flip() 成功反转了数组元素。

使用 numpy.flipud() 方法

我们可以使用numpy内置函数flipud()沿第0轴反转numpy数组的元素。该函数相当于数组[::-1]。以下是执行此操作的语法 –

numpy.flipud(m)

参数 m 表示一个输入数组,其元素必须反转。

示例

在此示例中,我们将使用 numpy.flipud() 函数反转数组元素。

import numpy as np

# creating array
arr = np.array([9, 3, 1, 6, 8])
print("The original array is: ", arr) 
print() 

# Reverse the elements of the array
result = np.flipud(arr)
print("The array after reversing the elements is:", result)

输出

The original array is: [9 3 1 6 8]
The array after reversing the elements is: [8 6 1 3 9]

这些是 python 中的几个内置函数,用于反转数组元素。

以上是使用内置函数的Python程序来反转数组的元素的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
在Python阵列上可以执行哪些常见操作?在Python阵列上可以执行哪些常见操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些类型的应用程序中,Numpy数组常用?在哪些类型的应用程序中,Numpy数组常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什么时候选择在Python中的列表上使用数组?您什么时候选择在Python中的列表上使用数组?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。

您如何在python列表中访问元素?您如何在python列表中访问元素?Apr 26, 2025 am 12:03 AM

toAccesselementsInapythonlist,useIndIndexing,负索引,切片,口头化。1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Python的科学计算中如何使用阵列?Python的科学计算中如何使用阵列?Apr 25, 2025 am 12:28 AM

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

您如何处理同一系统上的不同Python版本?您如何处理同一系统上的不同Python版本?Apr 25, 2025 am 12:24 AM

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

与标准Python阵列相比,使用Numpy数组的一些优点是什么?与标准Python阵列相比,使用Numpy数组的一些优点是什么?Apr 25, 2025 am 12:21 AM

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

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脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中