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ụ. Show
Phương pháp list.reverse()8 đảo ngược các yếu tố của danh sách. Thí dụ
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
Đầ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
Đầ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
Đầ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.
Thí dụCú pháp đảo ngược ()
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 ()
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ề
Một danh sách đảo ngược các mục có trong một đối tượng chuỗilist.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 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ỉnhlist.reverse()2 Đầu ra list.reverse()3 Trong ví dụ trên, chúng tôi đã sử dụng phương thức 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:
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.
Đả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. Python3Cá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 1Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']8 3Original 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 9Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']2 Linux macOS Windows1 Output: list.reverse()4 Chứng minh lỗi trong phương thức Reverse () the Error in reverse() MethodKhi 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. Python3Linux macOS Windows2 Original 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']2 Linux 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; Python3Original 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 1Original 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 3 9 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 9list.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 Python3Original 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 3 9 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 9list.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
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. |