Hướng dẫn dùng numpy searchsorted python - using numpy searchsorted python

numpy.SearchSorted (a, v, side = 'left', sorter = none) [nguồn]#searchsorted(a, v, side='left', sorter=None)[source]#

Tìm các chỉ số nơi các yếu tố nên được chèn để duy trì trật tự.

Tìm các chỉ số thành một mảng được sắp xếp sao cho các phần tử tương ứng trong V được chèn trước các chỉ số, thứ tự của A sẽ được bảo tồn.

Giả sử rằng A được sắp xếp:

cạnh

đã trả về chỉ số tôi thỏa mãn

bên trái

a[i-1] < v <= a[i]

bên phải

a[i-1] <= v < a[i]

Tham số arraya-d mảng_Lhea1-D array_like

Mảng đầu vào. Nếu người sắp xếp không phải là không, thì nó phải được sắp xếp theo thứ tự tăng dần, nếu không, người sắp xếp phải là một loạt các chỉ số sắp xếp nó.

varray_likearray_like

Các giá trị để chèn vào a.

bên {’trái,’ bên phải}, tùy chọn{‘left’, ‘right’}, optional

Nếu ’trái, chỉ số của vị trí phù hợp đầu tiên được tìm thấy. Nếu đúng, hãy trả lại chỉ số cuối cùng như vậy. Nếu không có chỉ số phù hợp, hãy trả lại 0 hoặc N (trong đó n là độ dài của a).

SORTER1-D ARRAY_LELE, TÙY CHỌN1-D array_like, optional

Mảng tùy chọn của các chỉ số số nguyên sắp xếp mảng a theo thứ tự tăng dần. Họ thường là kết quả của Argsort.

Mới trong phiên bản 1.7.0.

ReturnsIndiceInt hoặc mảng intsindicesint or array of ints

Mảng các điểm chèn có hình tương tự như V hoặc số nguyên nếu V là vô hướng.

Xem thêm

sort

Trả về một bản sao sắp xếp của một mảng.

histogram

Sản xuất biểu đồ từ dữ liệu 1-D.

Ghi chú

Tìm kiếm nhị phân được sử dụng để tìm các điểm chèn cần thiết.

Kể từ Numpy 1.4.0 searchsorted hoạt động với các mảng thực/phức tạp chứa các giá trị nan. Thứ tự sắp xếp nâng cao được ghi lại trong sort.

Hàm này sử dụng cùng một thuật toán như các hàm python

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
1 (
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
2) và
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
3 (
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
4), cũng được vector hóa trong đối số V.

Ví dụ

>>> np.searchsorted([1,2,3,4,5], 3)
2
>>> np.searchsorted([1,2,3,4,5], 3, side='right')
3
>>> np.searchsorted([1,2,3,4,5], [-10, 10, 2, 3])
array([0, 5, 1, 2])

& nbsp; mã số 2:function is used to find the indices into a sorted array arr such that, if elements are inserted before the indices, the order of arr would be still preserved. Here, binary search is used to find the required insertion indices.

a[i-1] < v <= a[i]2

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
9 histogram77
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
9histogram9searchsorted0
numpy.searchsorted(arr, num, side=’left’, sorter=None)

& nbsp; mã số 3:
arr : [array_like] Input array. If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it.
num : [array_like]The Values which we want to insert into arr.
side : [‘left’, ‘right’], optional.If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index. If there is no suitable index, return either 0 or N (where N is the length of a).
num : [array_like, Optional] array of integer indices that sort array a into ascending order. They are typically the result of argsort.

Trả về: [Chỉ số], mảng các điểm chèn có hình dạng giống như Num.[indices], Array of insertion points with the same shape as num.

Mã số 1: Làm việc

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
6
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
7

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
8
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
9
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  3
0
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  3
1
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  3
2223__2222225252222272722222929

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
1
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
2
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
3
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
4

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
5
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
9
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  3
5

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
1
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
2a[i-1] < v <= a[i]0a[i-1] < v <= a[i]1

a[i-1] < v <= a[i]2

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
9 a[i-1] < v <= a[i]4

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
1
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
2a[i-1] < v <= a[i]7a[i-1] < v <= a[i]8

Đầu ra:

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2

& nbsp; mã số 2:
Code #2 :

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
6
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
7

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
8
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
9
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  3
0
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  3
1
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  3
2223__2222225252222272722222929

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
1
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
2
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
3
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
4

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
5
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
9
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  3
5

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
1
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
2a[i-1] < v <= a[i]0histogram4

a[i-1] < v <= a[i]2

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
9 a[i-1] < v <= a[i]4

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
1
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
2a[i-1] < v <= a[i]7a[i-1] < v <= a[i]8

Đầu ra:

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  3

& nbsp; mã số 2:
Code #3 :

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
6
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
7

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
8
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
9
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  3
0
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  3
1
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  3
2223__2222225252222272722222929

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
1
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
2
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
3
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
4

a[i-1] < v <= a[i]2

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
9 histogram77
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
9histogram9searchsorted0

& nbsp; mã số 3:

a[i-1] < v <= a[i]2

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  4
Output indices to maintain sorted array :  2
9 a[i-1] < v <= a[i]4

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
1
Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]
2a[i-1] < v <= a[i]7a[i-1] < v <= a[i]8

Đầu ra:

Input array :  [2, 3, 4, 5, 6]
The number which we want to insert :  [4, 8, 0]
Output indices to maintain sorted array :  [2 5 0]