Hướng dẫn is there a reverse function in python? - có một chức năng đảo ngược trong python?

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về Phương thức Reversion () Danh sách Python với sự trợ giúp của các ví dụ.

Phương pháp

list.reverse()
8 đảo ngược các yếu tố của danh sách.

Thí dụ

# create a list of prime numbers
prime_numbers = [2, 3, 5, 7]

# reverse the order of list elements prime_numbers.reverse()

print('Reversed List:', prime_numbers) # Output: Reversed List: [7, 5, 3, 2]


Cú pháp của Danh sách đảo ngược ()

Cú pháp của phương thức

list.reverse()
8 là:

list.reverse()

Reverse () tham số

Phương pháp

list.reverse()
8 không có bất kỳ đối số nào.


Trả về giá trị từ Reverse ()

Phương thức

list.reverse()
8 không trả về bất kỳ giá trị nào. Nó cập nhật danh sách hiện có.


Ví dụ 1: Đảo ngược danh sách

# Operating System List
systems = ['Windows', 'macOS', 'Linux']
print('Original List:', systems)

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)

Đầu ra

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']

Có một số cách khác để đảo ngược một danh sách.


Ví dụ 2: Đảo ngược danh sách bằng cách sử dụng toán tử cắt lát

# Operating System List
systems = ['Windows', 'macOS', 'Linux']
print('Original List:', systems)

# Reversing a list	

# Syntax: reversed_list = systems[start:stop:step] reversed_list = systems[::-1]

# updated list print('Updated List:', reversed_list)

Đầu ra

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']

Có một số cách khác để đảo ngược một danh sách.

Ví dụ 2: Đảo ngược danh sách bằng cách sử dụng toán tử cắt lát

# Operating System List
systems = ['Windows', 'macOS', 'Linux']

# Printing Elements in Reversed Order

for o in reversed(systems): print(o)

Đầu ra

Linux
macOS
Windows

Trong Python, một hàm tích hợp có tên Reverse () được sử dụng để đảo ngược danh sách. Cách đơn giản và nhanh chóng này để đảo ngược danh sách đòi hỏi ít bộ nhớ. Cú pháp: list_name. Reversion () ở đây, list_name có nghĩa là bạn phải viết tên của danh sách, phải được đảo ngược.

Trong hướng dẫn này, bạn sẽ tìm hiểu về Python đã đảo ngược () với sự trợ giúp của các ví dụ.

Phương thức # Operating System List systems = ['Windows', 'macOS', 'Linux'] print('Original List:', systems) # List Reverse systems.reverse() # updated list print('Updated List:', systems)2 tính toán ngược lại của một đối tượng chuỗi đã cho và trả về nó dưới dạng danh sách.

seq_string = 'Python'

# reverse of a string print(list(reversed(seq_string)))

# Output: ['n', 'o', 'h', 't', 'y', 'P']


Thí dụ

Cú pháp đảo ngược ()

reversed(sequence_object)

Cú pháp của # Operating System List systems = ['Windows', 'macOS', 'Linux'] print('Original List:', systems) # List Reverse systems.reverse() # updated list print('Updated List:', systems)2 là:

tham số đảo ngược ()

  • Phương thức
    # Operating System List
    systems = ['Windows', 'macOS', 'Linux']
    print('Original List:', systems)
    
    

    # List Reverse systems.reverse()

    # updated list print('Updated List:', systems)
    2 có một tham số duy nhất:
    - an indexable object to be reversed (can be a tuple, string, list, range, etc.)

SEQUENCE_OBject - một đối tượng có thể lập chỉ mục được đảo ngược (có thể là một tuple, chuỗi, danh sách, phạm vi, v.v.): Since we can't index objects such as a set and a dictionary, they are not considered sequence objects.


Lưu ý: Vì chúng ta không thể lập chỉ mục các đối tượng như tập hợp và từ điển, chúng không được coi là đối tượng chuỗi.

đảo ngược () giá trị trả về

  • Phương thức
    # Operating System List
    systems = ['Windows', 'macOS', 'Linux']
    print('Original List:', systems)
    
    

    # List Reverse systems.reverse()

    # updated list print('Updated List:', systems)
    2 trả về:

Một danh sách đảo ngược các mục có trong một đối tượng chuỗi

list.reverse()
0

Đầu ra

list.reverse()
1

Ví dụ 1: Python đảo ngược () với các đối tượng trình tự tích hợp

Trong ví dụ trên, chúng tôi đã sử dụng phương thức

# Operating System List
systems = ['Windows', 'macOS', 'Linux']
print('Original List:', systems)

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)
2 với các đối tượng như tuple, phạm vi và danh sách.


Ví dụ 2: đảo ngược () với các đối tượng tùy chỉnh

list.reverse()
2

Đầu ra

list.reverse()
3

Trong ví dụ trên, chúng tôi đã sử dụng phương thức

# Operating System List
systems = ['Windows', 'macOS', 'Linux']
print('Original List:', systems)

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)
2 với một đối tượng tùy chỉnh
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
2 của lớp
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
3.

Ở đây, phương thức trả về thứ tự ngược của chuỗi trong danh sách nguyên âm.


Bài đọc được đề xuất:

  • Python str ()
  • Python iter ()

Trong Python, một hàm tích hợp có tên Reverse () được sử dụng để đảo ngược danh sách. Cách đơn giản và nhanh chóng này để đảo ngược danh sách đòi hỏi ít bộ nhớ. Cú pháp: list_name. Reversion () ở đây, list_name có nghĩa là bạn phải viết tên của danh sách, phải được đảo ngược. is an inbuilt method in the Python programming language that reverses objects of the List in place i.e. it doesn’t use any extra space but it just modifies the original list.

Danh sách Python đảo ngược () là một phương thức sẵn có trong ngôn ngữ lập trình Python đảo ngược các đối tượng của danh sách tại chỗ, nó không sử dụng bất kỳ không gian bổ sung nào nhưng nó chỉ sửa đổi danh sách ban đầu.list_name.reverse()

Cú pháp: list_name.reverse () There are no parameters.

Tham số: Không có tham số.The reverse() method does not return any value but reverses the given object from the list.

Đảo ngược danh sách bằng Danh sách đảo ngược ()

Ở đây chúng tôi đang đảo ngược danh sách bằng cách sử dụng hàm list () trong Python.

Python3

Các

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
1

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
2
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
3

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
4
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
6
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
7
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
8
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
9
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
8
# Operating System List
systems = ['Windows', 'macOS', 'Linux']

# Printing Elements in Reversed Order

for o in reversed(systems): print(o)

1
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
8
# Operating System List
systems = ['Windows', 'macOS', 'Linux']

# Printing Elements in Reversed Order

for o in reversed(systems): print(o)

3
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
8
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
7____38
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
7
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
0

# Operating System List
systems = ['Windows', 'macOS', 'Linux']

# Printing Elements in Reversed Order

for o in reversed(systems): print(o)

9

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
2
Linux
macOS
Windows
1

Output:  

list.reverse()
4

Chứng minh lỗi trong phương thức Reverse () the Error in reverse() Method

Khi bất cứ thứ gì khác ngoài danh sách được sử dụng thay cho danh sách, thì nó sẽ trả về một thuộc tính.

Python3

Linux
macOS
Windows
2
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
Linux
macOS
Windows
4

Linux
macOS
Windows
5

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
2
Linux
macOS
Windows
7

Output:  

list.reverse()
5

Ứng dụng thực tế

Đưa ra một danh sách các số, hãy kiểm tra xem danh sách có phải là một palindrom không. & Nbsp;

Python3

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
4
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
6
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
7
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
8
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
9
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
8
# Operating System List
systems = ['Windows', 'macOS', 'Linux']
print('Original List:', systems)

# Reversing a list	

# Syntax: reversed_list = systems[start:stop:step] reversed_list = systems[::-1]

# updated list print('Updated List:', reversed_list)
1
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
8
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
9
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
8
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
7
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
0

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
4
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
reversed(sequence_object)
3

# Operating System List
systems = ['Windows', 'macOS', 'Linux']

# Printing Elements in Reversed Order

for o in reversed(systems): print(o)

9

reversed(sequence_object)
5
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
4
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
reversed(sequence_object)
9

list.reverse()
00
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
2
list.reverse()
02
list.reverse()
03
list.reverse()
04

list.reverse()
05
list.reverse()
06

list.reverse()
00
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
2
list.reverse()
02
list.reverse()
10
list.reverse()
04

Output:  

list.reverse()
6

Python3

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
4
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
list.reverse()
14
list.reverse()
022____116
list.reverse()
04

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
4
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
reversed(sequence_object)
3

# Operating System List
systems = ['Windows', 'macOS', 'Linux']

# Printing Elements in Reversed Order

for o in reversed(systems): print(o)

9

reversed(sequence_object)
5
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
4
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
reversed(sequence_object)
9

list.reverse()
00
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
2
list.reverse()
02
list.reverse()
03
list.reverse()
04

list.reverse()
05
list.reverse()
06

list.reverse()
00
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
2
list.reverse()
02
list.reverse()
10
list.reverse()
04

Output:

list.reverse()
6

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
4
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
list.reverse()
14
list.reverse()
022____116
list.reverse()
04
Palindrome-sequence that reads the same backward as forwards.


Làm thế nào chức năng đảo ngược hoạt động trong Python?

Python đảo ngược () Phương thức đảo ngược () tính toán ngược lại của một đối tượng chuỗi đã cho và trả về nó dưới dạng một danh sách.

Làm thế nào để bạn đảo ngược dữ liệu trong Python?

Trong Python, một hàm tích hợp có tên Reverse () được sử dụng để đảo ngược danh sách.Cách đơn giản và nhanh chóng này để đảo ngược danh sách đòi hỏi ít bộ nhớ.Cú pháp: list_name.Reversion () ở đây, list_name có nghĩa là bạn phải viết tên của danh sách, phải được đảo ngược.a built-in function called reverse() is used to reverse the list. This simple and quick way to reverse a list requires little memory. Syntax: list_name. reverse() Here, list_name means you have to write the list's name, which has to be reversed.