Chúng ta thường gặp các tình huống cần giảm kích thước của danh sách bằng cách cắt bớt k phần tử cuối cùng của danh sách. Điều này có ứng dụng trong lập trình hàng ngày khi đôi khi chúng tôi yêu cầu lấy tất cả các danh sách có kích thước tương tự hoặc xóa một vài bản ghi cuối cùng khỏi danh sách. Hãy thảo luận về một vài cách mà nhiệm vụ này có thể được thực hiện
Sử dụng len[] + cắt danh sách để xóa K phần tử cuối cùng của danh sách
Cắt danh sách có thể thực hiện nhiệm vụ cụ thể này, trong đó chúng ta chỉ cắt len[list] – K phần tử đầu tiên có trong danh sách và do đó loại bỏ K phần tử cuối cùng
Ở đây, chúng tôi tìm độ dài của danh sách hiện tại và tính toán độ dài sẽ có sau khi xóa n phần tử [len_l – l], sau đó sử dụng phép cắt, chúng tôi tạo một danh sách sao chép, trong đó nó đã xóa n phần tử cuối cùng
Python3
# initializing list
test_list
=
[
1
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0_______0_______1
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]3
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]5
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]7
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]9
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]0
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]1
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]2
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]3
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]4
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]5
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]6
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]7
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]8
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]9_______178_______
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]5
List after removing elements: [1, 7, 6]2
List after removing elements: [1, 7, 6]3
List after removing elements: [1, 7, 6]4
=
List after removing elements: [1, 7, 6]6
List after removing elements: [1, 7, 6]7
List after removing elements: [1, 7, 6]8
List after removing elements: [1, 7, 6]9
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]0
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]1
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]2
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]3
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]4
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]5
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]6
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]7
Đầu ra
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]
Sử dụng kỹ thuật cắt danh sách Phủ định để loại bỏ K phần tử cuối cùng của danh sách
Chúng ta có thể thực hiện tác vụ cụ thể này bằng cách sử dụng phép cắt danh sách phủ định, trong đó chúng ta bắt đầu xóa các phần tử khỏi chỉ mục cuối cùng của danh sách và do đó xóa tất cả các phần tử K khỏi chỉ mục cuối cùng. Nếu giá trị chỉ mục âm lớn hơn độ dài của danh sách, nó sẽ trả về một danh sách trống
Python3
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]8
test_list
=
[
1
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0_______0_______1
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]3
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]5
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]7
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]9
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]0
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]1
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]2_______10_______3
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]4
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]5
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]6
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]7
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]8
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]9_______178_______
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]5
=
5
List after removing elements: [1, 7, 6]3
List after removing elements: [1, 7, 6]4
=
List after removing elements: [1, 7, 6]6
List after removing elements: [1, 7, 6]9
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]0
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]1
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]2_______10_______3
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]4
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]5
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]6
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]7
đầu ra
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]
Sử dụng Phương thức Python List pop[] để xóa K phần tử cuối cùng của danh sách
Phương thức pop[] sẽ xóa phần tử cuối cùng khỏi danh sách, vì vậy để xóa k phần tử cuối cùng khỏi Danh sách Python, chúng ta cần thực hiện thao tác pop[] k lần
Python3
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]8
________177____178_______ [
___1
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0_______180_______5
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]3
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0_______0_______5
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]7
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]9
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]0
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]1
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]2_______10_______3
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]4
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]5
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]6
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]7
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]8
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]9_______178_______
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]5
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]16
List after removing elements: [1, 7, 6]3
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]18
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]19
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]20
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]21_______0_______22
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]23
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]24
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]25
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]26
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]1
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]2_______10_______3
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]4
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]5
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]6
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]7
đầu ra
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]
Sử dụng từ khóa del và cắt danh sách để xóa K phần tử cuối cùng của danh sách
Ở đây chúng tôi trả về một phạm vi được cắt lát và xóa nó khỏi danh sách bằng Python del từ khóa
Python3
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]8
________177____178_______ [
___1
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0_______180_______5
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]3
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0_______0_______5
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]7
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]9
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]50
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]9_______178_______
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]5
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]54
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]55
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]56
List after removing elements: [1, 7, 6]9
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]5_______0_______59
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]2_______10_______3
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]62
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]63
đầu ra
List after removing elements: [1, 7, 6]
Sử dụng itertools
Một cách tiếp cận khác để loại bỏ k phần tử cuối cùng của danh sách là sử dụng hàm islice của mô-đun itertools. Hàm islice trả về một trình lặp tạo ra các phần tử được chọn từ trình lặp đầu vào. Chúng ta có thể sử dụng hàm này để tạo một trình vòng lặp mới tạo ra tất cả các phần tử của danh sách trừ k phần tử cuối cùng, sau đó sử dụng hàm list để tạo một danh sách mới từ trình vòng lặp
Đây là một ví dụ về cách điều này có thể được thực hiện
Python3
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]64
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]65
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]8
test_list
=
[
1
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0_______0_______1
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]3
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]5
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]7
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]9
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]0
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]1
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]2_______10_______3
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]85
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]63
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]8
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]9_______178_______
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]5
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]91
List after removing elements: [1, 7, 6]4
=
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]94
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]95
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]96
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]0
List after removing elements: [1, 7, 6]7
List after removing elements: [1, 7, 6]8
List after removing elements: [1, 7, 6]9
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]01
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]1
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]2_______10_______3
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]05
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]06
The original list is : [1, 7, 6, 3, 5, 8] The list after removing last K elements : [1, 7, 6]07
Đầu ra
The original list is: [1, 4, 6, 3, 5, 8] The list after removing last K elements: [1, 4, 6]
Độ phức tạp về thời gian của cách tiếp cận islice để loại bỏ k phần tử cuối cùng của danh sách là O[n], trong đó n là độ dài của danh sách. Điều này là do hàm islice phải lặp qua tất cả n phần tử của danh sách đầu vào để tạo ra trình vòng lặp đầu ra mong muốn. Độ phức tạp không gian của cách tiếp cận islice cũng là O[n]