Hướng dẫn how do you remove negative elements from an array in python? - làm cách nào để loại bỏ các phần tử âm khỏi một mảng trong python?

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]
0np.array0____51 np.array2
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]
6np.array5

Đầ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]
2test_list 9=0test_list 9=2 =3
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
7=5

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
0np.array0____51 np.array2
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]
6np.array5

Đầ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]
2test_list 9=0test_list 9=2 =3
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
7=5

Phươ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
43

test_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
0
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
1
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
2
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
3__12

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
0test_list 9
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
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
65
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
66

Các

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
1
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
45
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
3
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
47test_list 9
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
24
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
51
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
52

The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]


Bài Viết Liên Quan

Chủ Đề