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 Đư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ừ ..
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ụ.
tham số đảo ngược ()Phương thức 2 có một tham số duy nhất:
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 2 trả về:
Ví dụ 1: Python đảo ngược () với các đối tượng trình tự tích hợplist.reverse()0 Đầu ra list.reverse()1 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.Khi sử dụng phương thức 2 với các đối tượng này, chúng ta cần sử dụng phương thức 9 để chuyển đổi đầu ra từ phương thức 2 sang 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:
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.
Đả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
Python đảo ngược là gì?Phương thức Python đảo ngược () trả về một trình lặp lại truy cập vào chuỗi đã cho theo thứ tự ngược lại.returns an iterator that accesses the given sequence in the reverse order.
Dữ liệu đảo ngược () làm gì?Phương thức đảo ngược () đảo ngược một mảng tại chỗ và trả về tham chiếu đến cùng một mảng, phần tử mảng đầu tiên bây giờ trở thành phần cuối cùng và phần tử mảng cuối cùng trở thành phần đầu tiên.Nói cách khác, thứ tự các phần tử trong mảng sẽ được chuyển về hướng ngược lại với điều kiện đã nêu trước đó.reverses an array in place and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated.
Làm thế nào để bạn đảo ngược một mã trong Python?Đư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ừ .. 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 .. |