Python List reverse() és un mètode integrat al llenguatge de programació Python que inverteix els objectes de la llista al seu lloc, és a dir, no utilitza cap espai addicional, sinó que només modifica la llista original.
Python List reverse() Sintaxi
Sintaxi: nom_lista.reverse()
Paràmetres: No hi ha paràmetres.
Devolucions: El mètode reverse() no retorna cap valor però inverteix l'objecte donat de la llista.
Llista reverse() a l'exemple de Python
Invertir una llista utilitzant list reverse()
Aquí estem invertint la llista utilitzant la funció list reverse() a Python.
Python 3
# Python3 program to demonstrate the> # use of reverse method> > # a list of numbers> list1> => [> 1> ,> 2> ,> 3> ,> 4> ,> 1> ,> 2> ,> 6> ]> list1.reverse()> print> (list1)> # a list of characters> list2> => [> 'a'> ,> 'b'> ,> 'c'> ,> 'd'> ,> 'a'> ,> 'a'> ]> list2.reverse()> print> (list2)> |
>
css flotant
>
Sortida:
[6, 2, 1, 4, 3, 2, 1] ['a', 'a', 'd', 'c', 'b', 'a']>
Error al revés () Mètode
Quan s'utilitza qualsevol cosa que no sigui llista en lloc de llista, retorna un AttributeError.
Python 3
dempeus
# Python3 program to demonstrate the> # error in reverse() method> > # error when string is used in place of list> string> => 'abgedge'> string.reverse()> print> (string)> |
>
>
Sortida:
Traceback (most recent call last): File '/home/b3cf360e62d8812babb5549c3a4d3d30.py', line 5, in string.reverse() AttributeError: 'str' object has no attribute 'reverse'>
Invertir una llista mitjançant l'operador de tall
En aquest exemple, el [::-1] operador de tall crea una nova llista que és la revés de la meva_llista.
Python 3
my_list> => [> 1> ,> 2> ,> 3> ,> 4> ,> 5> ]> reversed_list> => my_list[::> -> 1> ]> print> (reversed_list)> |
>
>
Sortida:
[5, 4, 3, 2, 1]>
Revertir una subllista mitjançant Slicing
En aquest exemple, estem invertint una subllista de l'índex 1 al 3 mitjançant l'operador [::-1].
Python 3
js onload
my_list> => [> 1> ,> 2> ,> 3> ,> 4> ,> 5> ]> print> (> 'Original list:'> , my_list)> my_list[> 1> :> 4> ]> => my_list[> 1> :> 4> ][::> -> 1> ]> print> (> 'Reversed sublist:'> , my_list)> |
>
>
Sortida:
Original list: [1, 2, 3, 4, 5] Reversed sublist: [1, 4, 3, 2, 5]>
Accés als elements en ordre invers
En aquest exemple, estem recorrent la llista en ordre invers.
Python 3
codi java declaració if else
my_list> => [> 1> ,> 2> ,> 3> ,> 4> ,> 5> ]> for> element> in> reversed> (my_list):> > print> (element)> |
>
>
Sortida:
5 4 3 2 1>
Revertir una llista de tipus de dades mixtes
En aquest exemple, estem invertint el llista de tipus de dades mixtes amb la funció reverse().
Python 3
my_list> => [> 1> ,> 'apple'> ,> 2.5> ,> True> ]> print> (> 'Original list:'> , my_list)> my_list.reverse()> print> (> 'Reversed list:'> , my_list)> |
>
>
Sortida:
Original list: [1, 'apple', 2.5, True] Reversed list: [True, 2.5, 'apple', 1]>
Aplicació pràctica
Donada una llista de números, comproveu si la llista és un palíndrom .
Python 3
# Python3 program for the> # practical application of reverse()> list_arr> => [> 1> ,> 2> ,> 3> ,> 2> ,> 1> ]> list_string> => list> (> 'naman'> )> # store a copy of list> list2> => list_arr.copy()> list3> => list_string.copy()> # reverse the list> list2.reverse()> list3.reverse()> # compare reversed and original list> if> list_arr> => => list2:> > print> (list_arr,> ': Palindrome'> )> else> :> > print> (list_arr,> ': Not Palindrome'> )> # compare reversed and original list> if> list_string> => => list3:> > print> (list_string,> ': Palindrome'> )> else> :> > print> (list_string,> ': Not Palindrome'> )> |
>
prova de capturar en java
>
Sortida
[1, 2, 3, 2, 1] : Palindrome ['n', 'a', 'm', 'a', 'n'] : Palindrome>
Nota: Palíndrom: seqüència que es llegeix igual cap enrere que cap endavant.