Programme Python utilisant une fonction intégrée pour inverser les éléments du tableau

PHPz
Libérer: 2023-09-06 15:49:08
avant
1196 Les gens l'ont consulté

Programme Python utilisant une fonction intégrée pour inverser les éléments du tableau

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

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

输入输出场景

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

Input array:
[3, 5, 1, 4, 0, 2]
Output array:
[2, 0, 4, 1, 5, 3]
Copier après la connexion

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

使用内置函数reverse()

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

reversed(seq) 
Copier après la connexion

该函数将迭代器作为唯一参数,并返回一个反向迭代器。该函数返回一个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)
Copier après la connexion

输出

The original array is:  [5, 2, 1, 6, 8]
The array after reversing the elements is: [8, 6, 1, 2, 5]
Copier après la connexion
Copier après la connexion

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

使用 list.reverse() 函数

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

list_obj.reverse() 
Copier après la connexion

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)
Copier après la connexion

输出

The original array is:  [5, 2, 1, 6, 8]
The array after reversing the elements is: [8, 6, 1, 2, 5]
Copier après la connexion
Copier après la connexion

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

使用 numpy.flip() 函数

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

numpy.flip(m, axis=None)
Copier après la connexion

哪里,

  • 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)
Copier après la connexion

输出

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]
Copier après la connexion

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

使用 numpy.flipud() 方法

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

numpy.flipud(m)
Copier après la connexion

参数 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)
Copier après la connexion

输出

The original array is: [9 3 1 6 8]
The array after reversing the elements is: [8 6 1 3 9]
Copier après la connexion

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

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:tutorialspoint.com
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!