Hướng dẫn remove negative values from array python - xóa các giá trị âm khỏi python mảng

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]

Phương thức số 3: Sử dụng danh sách [], map [], startswith [] Phương thứcMethod #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]

Phương thức số 3: Sử dụng danh sách [], map [], startswith [] Phương thức

Python3

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

Cá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

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

Cá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
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

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
69
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

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


Làm thế nào để bạn loại bỏ các giá trị âm từ một mảng trong Python?

Cho một mảng mảng [] có kích thước n, nhiệm vụ là loại bỏ tất cả các phần tử âm khỏi mảng này ...
Tạo một Vector Newarr để chỉ lưu trữ các yếu tố tích cực ..
Bây giờ đi qua mảng mảng và đẩy phần tử tích cực trong newarr ..
Trả lại Newarr làm câu trả lời ..

Làm thế nào để bạn thoát khỏi một tiêu cực trong Python?

Trong Python, các số dương có thể được thay đổi thành số âm với sự trợ giúp của phương thức được xây dựng được cung cấp trong thư viện Python có tên ABS [].Khi ABS [] được sử dụng, nó chuyển đổi số âm thành dương.abs []. When abs [] is used, it converts negative numbers to positive.

Làm thế nào để bạn lọc các số âm trong một mảng?

bộ lọc [trích xuất tập hợp con của mảng].
Bản đồ [để lặp lại mảng].
giảm [để nhận giá trị đơn từ mảng i.e tổng của mảng].

Làm thế nào để bạn thay đổi giá trị âm trong Python?

Linked..
Thay thế các phần tử âm ndarry NUMPY bằng 0 [không].
Điều kiện thay thế các phần tử trong một mảng trong Python ..
-2. ... .
Sử dụng Arctan / ArcTan2 để vẽ A từ 0 đến 2π.
Mảng Numpy - Thay thế một số giá trị nhất định ..
Nhanh hơn cho vòng lặp chỉ với trong Python ..

Bài Viết Liên Quan

Chủ Đề