Nếu hiệu suất là quan trọng, bạn có thể tận dụng thực tế là np.array
của bạn được sắp xếp và sử dụng Numpy.SearchSorted
Ví dụ:
In [8]: x[np.searchsorted[x, 0] :]
Out[8]: array[[ 0. , 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10. , 14. , 16.2]]
In [9]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.47 us per loop
In [10]: %timeit x[x >= 0]
100000 loops, best of 3: 4.5 us per loop
Sự khác biệt về hiệu suất sẽ tăng khi kích thước của mảng tăng lên vì np.searchsorted
thực hiện tìm kiếm nhị phân là O [log n] so với tìm kiếm tuyến tính O [n] mà x >= 0
đang thực hiện.
In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
Đôi khi, trong khi làm việc với danh sách Python, chúng ta có thể gặp vấn đề trong đó chúng ta cần xóa tất cả các yếu tố tiêu cực khỏi danh sách. Loại vấn đề này có thể có ứng dụng trong nhiều lĩnh vực như lập trình trường học và phát triển web. Hãy để thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.
Đầu vào: test_list = [6, 4, 3] Đầu ra: [6, 4, 3] Đầu vào: test_list = [-6, -4] Đầu ra: [] : test_list = [6, 4, 3] Output : [6, 4, 3] Input : test_list = [-6, -4] Output : []
Phương pháp số 1: Sử dụng danh sách Hiểu biết kết hợp các hàm trên có thể được sử dụng để giải quyết vấn đề này. Trong đó, chúng tôi thực hiện nhiệm vụ loại bỏ các phần tử tiêu cực bằng cách lặp trong một lớp lót bằng cách sử dụng danh sách hiểu & nbsp; The combination of above functions can be used to solve this problem. In this, we perform the task of removing negative elements by iteration in one liner using list comprehension
Python3
Các
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]0
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]3
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]4
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]5
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]6
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]7
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]8
=
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]0
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]3
test_list
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]5
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]0
np.array
0____51 np.array
2The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]5
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]6
np.array
5Đầu ra: & nbsp;
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]
& nbsp; Phương pháp số 2: Sử dụng Filter [] + lambda Sự kết hợp của các hàm trên cũng có thể cung cấp một giải pháp thay thế cho vấn đề này. Trong đó, chúng tôi mở rộng logic của việc giữ lại tích cực được hình thành bằng hàm lambda và mở rộng bằng Filter []. & NBSP;Method #2 : Using filter[] + lambda The combination of above functions can also offer an alternative to this problem. In this, we extend logic of retaining positive formed using lambda function and extended using filter[].
Python3
Các
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]0
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]1
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]2
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]3
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]4
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]5
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]6
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]7
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]8
=
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]2
test_list
9=
0test_list
9=
2 =
3The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]7
=
5The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]0
np.array
0____51 np.array
2The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]5
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]6
np.array
5
Đầu ra: & nbsp;
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]
& nbsp; Phương pháp số 2: Sử dụng Filter [] + lambda Sự kết hợp của các hàm trên cũng có thể cung cấp một giải pháp thay thế cho vấn đề này. Trong đó, chúng tôi mở rộng logic của việc giữ lại tích cực được hình thành bằng hàm lambda và mở rộng bằng Filter []. & NBSP;
Python3
Các
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]8
=
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]2
test_list
9=
0test_list
9=
2 =
3The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]7
=
5Phương thức số 3: Sử dụng danh sách [], map [], startswith [] Phương thức
In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
41=
In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
43test_list
=
In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
0In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
1In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
2In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
3__12The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]0
test_list
9In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
29 The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]5
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]6
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]7
In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
65In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
66Các
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
45In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
3
47In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
test_list
9The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
7
24In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
51In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
52In [11]: x = np.arange[-1000, 1000]
In [12]: %timeit x[np.searchsorted[x, 0] :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
The original list is : [5, 6, -3, -8, 9, 11, -12, 2] List after filtering : [5, 6, 9, 11, 2]