Hướng dẫn dùng numpy searchsorted python - using numpy searchsorted python
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:
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_likeCác giá trị để chèn vào a. bên {’trái,’ bên phải}, tùy chọn{‘left’, ‘right’}, optionalNế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, optionalMả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 intsMả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 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 : 21 ( Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 22) và Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 23 ( Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 24), 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.
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 : 26 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 27 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 28 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 29 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 30 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 31 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 32223__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 : 29 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 35 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 a[i-1] < v <= a[i] 0a[i-1] < v <= a[i] 1
Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 29 a[i-1] < v <= a[i] 4Input 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 a[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: Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 26 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 27 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 28 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 29 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 30 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 31 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 32223__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 : 29 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 35 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 a[i-1] < v <= a[i] 0histogram 4
Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 29 a[i-1] < v <= a[i] 4Input 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 a[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: Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 26 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 27 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 28 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 29 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 30 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 31 Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 32223__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 Output indices to maintain sorted array : 29 histogram 77Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 29 histogram 9searchsorted 0& nbsp; mã số 3:
Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 29 a[i-1] < v <= a[i] 4Input 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 a[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] |