Nhiệm vụ là thực hiện hoạt động loại bỏ tất cả các lần xuất hiện của một mục/phần tử nhất định có trong danh sách. & NBSP;
Examples:
Đầu vào: 1 1 2 3 4 5 1 2 1 & nbsp;1 1 2 3 4 5 1 2 1
Đầu ra: 2 3 4 5 2 & nbsp;2 3 4 5 2
Giải thích: Danh sách đầu vào là [1, 1, 2, 3, 4, 5, 1, 2] và mục sẽ được xóa là 1. Sau khi xóa mục, danh sách đầu ra là [2, 3, 4, 5, 2] & nbsp;The input list is [1, 1, 2, 3, 4, 5, 1, 2] and the item to be removed is 1. After removing the item, the output list is [2, 3, 4, 5, 2]
Đầu vào: 5 6 7 8 9 10 7 & nbsp;5 6 7 8 9 10 7
Đầu ra: 5 6 8 9 105 6 8 9 10
Trong bài viết này, chúng ta sẽ thấy cách thực hiện nhiệm vụ này theo 3 cách:
- Sử dụng danh sách hiểu
- Sử dụng Filter [] và __ne__
- Sử dụng Remove []
Phương pháp 1: Sử dụng danh sách Hiểu danh sách Có thể sử dụng danh sách để thực hiện nhiệm vụ này, trong đó chúng tôi chỉ kiểm tra một trận đấu và xây dựng lại danh sách mà không cần phần tử đích. Chúng ta có thể tạo ra một nhóm phụ của các yếu tố trong danh sách thỏa mãn một điều kiện nhất định. & NBSP;Using list comprehension The list comprehension can be used to perform this task in which we just check for a match and reconstruct the list without the target element. We can create a sublist of those elements in the list that satisfies a certain condition.
Python3
def
remove_items[test_list, item]:
res
=
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]8
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]9
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]3
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]1______7
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]4
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]5
Các
remove_items[test_list, item]:
3=
def
0
remove_items[test_list, item]:
7remove_items[test_list, item]:
8remove_items[test_list, item]:
9
0
1
2
res
__
6
remove_items[test_list, item]:
7remove_items[test_list, item]:
8res
0
0
1res
3
Đầu ra
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]
Phương pháp 2: Sử dụng Filter [] và __ne__ Chúng tôi lọc các mục đó của danh sách không bằng __ne__ cho mục. & NBSP;Using filter[] and __ne__ We filter those items of the list which are not equal __ne__ to the item.
Python3
def
remove_items[test_list, item]:
res
=
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]8
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]9
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]3
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]1______7
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]4
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]5
Các
remove_items[test_list, item]:
3=
def
0
remove_items[test_list, item]:
7remove_items[test_list, item]:
8remove_items[test_list, item]:
9
0
1
2
res
__
6
remove_items[test_list, item]:
7remove_items[test_list, item]:
8res
0
0
1res
3
Đầu ra
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]
Phương pháp 2: Sử dụng Filter [] và __ne__ Chúng tôi lọc các mục đó của danh sách không bằng __ne__ cho mục. & NBSP;Using remove[] In this method, we iterate through each item in the list, and when we find a match for the item to be removed, we will call remove[] function on the list.
Python3
def
remove_items[test_list, item]:
res
=
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]8
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]9
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]32
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]33
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]3
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]1______7
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]4
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]5
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]3
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]1______7
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]4
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]5
Các
remove_items[test_list, item]:
3=
def
0
remove_items[test_list, item]:
7remove_items[test_list, item]:
8remove_items[test_list, item]:
9
0
1
2
res
__
6
remove_items[test_list, item]:
7remove_items[test_list, item]:
8res
0
0
1res
3
Đầu ra
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]
Phương pháp 2: Sử dụng Filter [] và __ne__ Chúng tôi lọc các mục đó của danh sách không bằng __ne__ cho mục. & NBSP;
Python3
res
=
res
9remove_items[test_list, item]:
8=
1=
2
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]96
=
def
0
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]8
The original list is : [1, 3, 4, 6, 5, 1] The list after performing the remove operation is : [3, 4, 6, 5]9
remove_items[test_list, item]:
7
The original list is :[1, 3, 4, 6, 5, 1] The list after performing the remove operation is :[3, 4, 6, 5]11