Hướng dẫn how do you reverse data in python? - làm thế nào để bạn đảo ngược dữ liệu 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 thức

list.reverse()
4 đả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()
4 là:

list.reverse()

Reverse () tham số

Phương pháp

list.reverse()
4 không lấy bất kỳ đối số nào.


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

Phương thức

list.reverse()
4 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

Python

Example: 

Input : list = [4, 5, 6, 7, 8, 9]
Output : [9, 8, 7, 6, 5, 4] 

list.reverse()41 list.reverse()42list.reverse()43 list.reverse()44

Is

Python3

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)
3
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
0
# Operating System List
systems = ['Windows', 'macOS', 'Linux']
print('Original List:', systems)

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)
3
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
2____33

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']
5
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
6
# 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
# Operating System List
systems = ['Windows', 'macOS', 'Linux']
print('Original List:', systems)

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)
3
# 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)
3
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
6
# 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)
5
# 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)
6

Output:

Using reverse()  [15, 14, 13, 12, 11, 10]
Using reversed()  [10, 11, 12, 13, 14, 15]

Đảo ngược danh sách bằng hàm chèn ()

Trong phương thức này, chúng tôi không đảo ngược danh sách tại chỗ (sửa đổi danh sách ban đầu) cũng như không tạo bất kỳ bản sao nào của danh sách. Thay vào đó, chúng tôi tiếp tục chèn các mục ở chỉ mục 0 của danh sách, điều này sẽ tự động đảo ngược danh sách.

Python3

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)
3
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
0
# Operating System List
systems = ['Windows', 'macOS', 'Linux']
print('Original List:', systems)

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)
3
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
2____33

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

# Printing Elements in Reversed Order

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

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

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)
0
# Operating System List
systems = ['Windows', 'macOS', 'Linux']

# Printing Elements in Reversed Order

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

4

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

# Printing Elements in Reversed Order

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

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

# Printing Elements in Reversed Order

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

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

# Printing Elements in Reversed Order

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

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

# Printing Elements in Reversed Order

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

8

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

# Printing Elements in Reversed Order

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

9
Linux
macOS
Windows
0
Linux
macOS
Windows
1
Linux
macOS
Windows
2

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

Đầu ra

list.reverse()
0

Đảo ngược một danh sách bằng kỹ thuật cắt lát

Trong kỹ thuật này, một bản sao của danh sách được thực hiện và danh sách không được sắp xếp tại chỗ. Tạo một bản sao đòi hỏi nhiều không gian hơn để giữ tất cả các yếu tố hiện có. Điều này làm cạn kiệt bộ nhớ nhiều hơn. Ở đây chúng tôi đang sử dụng kỹ thuật cắt lát để đảo ngược danh sách của chúng tôi trong Python.

Python3

Linux
macOS
Windows
5
Linux
macOS
Windows
6

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

# Printing Elements in Reversed Order

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

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

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)
0
Input : list = [4, 5, 6, 7, 8, 9]
Output : [9, 8, 7, 6, 5, 4] 
0
Input : list = [4, 5, 6, 7, 8, 9]
Output : [9, 8, 7, 6, 5, 4] 
1
Input : list = [4, 5, 6, 7, 8, 9]
Output : [9, 8, 7, 6, 5, 4] 
2
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
3

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

# Printing Elements in Reversed Order

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

9
Input : list = [4, 5, 6, 7, 8, 9]
Output : [9, 8, 7, 6, 5, 4] 
5
Input : list = [4, 5, 6, 7, 8, 9]
Output : [9, 8, 7, 6, 5, 4] 
6

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

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

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)
3
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
0
# Operating System List
systems = ['Windows', 'macOS', 'Linux']
print('Original List:', systems)

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)
3
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
2____33

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
list.reverse()
03

Đầu ra

list.reverse()
0

Đảo ngược danh sách bằng cách sử dụng danh sách hiểu

Trong kỹ thuật này, danh sách không được sắp xếp tại chỗ. Một bản sao của mảng ban đầu là không cần thiết. Chúng tôi sử dụng danh sách hiểu để đảo ngược mảng và trả về danh sách.

Chúng tôi tìm thấy độ dài của mảng và sau đó lặp qua nó bằng phạm vi. Bây giờ, để thay thế phần tử cuối cùng bằng đầu tiên, chúng tôi trừ độ dài của danh sách ban đầu bằng chỉ mục của trình lặp.

Python3

list.reverse()
04___

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

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)
0
list.reverse()
21
list.reverse()
222____123
Input : list = [4, 5, 6, 7, 8, 9]
Output : [9, 8, 7, 6, 5, 4] 
1
list.reverse()
25

Các

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
list.reverse()
40

Đầu ra

list.reverse()
0

Đảo ngược danh sách bằng phương pháp giảm

Trong kỹ thuật này, danh sách lặp lại trên từng phần tử và nối phần tử vào mặt trước của danh sách và cuối cùng chúng ta nhận được mảng theo thứ tự ngược lại. Tại mỗi lần lặp, chúng tôi chuyển đổi mục thành danh sách và kết hợp mảng trước đó với phần tử phía trước thành danh sách phần tử hiện tại.

Python

list.reverse()
41
list.reverse()
42
list.reverse()
43
list.reverse()
44

list.reverse()
04___

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

# List Reverse systems.reverse()

# updated list print('Updated List:', systems)
0
list.reverse()
21
list.reverse()
222____123
Input : list = [4, 5, 6, 7, 8, 9]
Output : [9, 8, 7, 6, 5, 4] 
1
list.reverse()
25

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']
5
list.reverse()
40

Output:

list.reverse()
0

Các

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.computes the reverse of a given sequence object and returns it in the form of a list.

Làm thế nào để bạn đảo ngược văn bản trong Python?

Làm thế nào để đảo ngược một chuỗi trong Python ?..
Sử dụng cho vòng lặp ..
Sử dụng trong khi vòng lặp ..
Sử dụng toán tử lát cắt ..
Sử dụng hàm đảo ngược () ..
Sử dụng đệ quy ..