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 Orderfor o in reversed(systems): print(o)
Đầu ra
Linux macOS WindowsĐưa ra dưới đây là các bước cần tuân thủ để giải quyết vấn đề này ..
Tách từng từ trong một chuỗi đã cho bằng phương thức Split () của loại dữ liệu chuỗi trong Python ..
Đảo ngược danh sách phân tách từ ..
seq_string = 'Python'# reverse of a string print(list(reversed(seq_string)))
# Output: ['n', 'o', 'h', 't', 'y', 'P']In các từ của danh sách, ở dạng chuỗi sau khi tham gia từng từ với không gian bằng cách sử dụng. tham gia () phương thức trong python ..
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ụ.
reversed(sequence_object)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:- 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.) - an indexable object to be reversed (can be a tuple, string, list, range, etc.)
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.: Since we can't index objects such as a set and a dictionary, they are not considered sequence objects.
đả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
Ví dụ 1: Python đảo ngược () với các đối tượng trình tự tích hợp
list.reverse()0Đầu ra
list.reverse()1Trong 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.Khi 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 này, chúng ta cần 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)9 để chuyển đổi đầu ra từ 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 sang 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()3Trong 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 ()
In các từ của danh sách, ở dạng chuỗi sau khi tham gia từng từ với không gian bằng cách sử dụng. tham gia () phương thức trong python .. 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']2Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']3
Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']4Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']5 Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']6Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']7Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']8Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']9Original 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)
1Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']8# Operating System List systems = ['Windows', 'macOS', 'Linux'] # Printing Elements in Reversed Orderfor o in reversed(systems): print(o)
3Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']8Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']7____38Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']7Original 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)
9Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']2Linux macOS Windows1
Output:
list.reverse()4Chứ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 Windows2Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']5 Linux macOS Windows4
Linux macOS Windows5
Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']2Linux macOS Windows7
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']4Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']5 Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']6Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']7Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']8Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']9Original 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)1Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']8Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']9Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']8Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']7Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']0Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']4Original 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)
9reversed(sequence_object)5 Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']4Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']5Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']5 reversed(sequence_object)9
list.reverse()00Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']2list.reverse()02list.reverse()03list.reverse()04
list.reverse()05list.reverse()06
list.reverse()00Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']2list.reverse()02list.reverse()10list.reverse()04
Output:
list.reverse()6Python3
Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']4Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']5 list.reverse()14list.reverse()022____116list.reverse()04
Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']4Original 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)
9reversed(sequence_object)5 Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']4Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']5Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']5 reversed(sequence_object)9
list.reverse()00Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']2list.reverse()02list.reverse()03list.reverse()04
list.reverse()05list.reverse()06
list.reverse()00Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']2list.reverse()02list.reverse()10list.reverse()04
Output:
list.reverse()6Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']4Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']5 list.reverse()14list.reverse()022____116list.reverse()04 Palindrome-sequence that reads the same backward as forwards.