Hướng dẫn dùng np searchsorted python


Searching Arrays

You can search an array for a certain value, and return the indexes that get a match.

Nội dung chính

  • Searching Arrays
  • Search Sorted
  • Search From the Right Side
  • Multiple Values
  • Test Yourself With Exercises
  • Tìm kiếm (Array Search) trong mảng Numpy
  • 1. Tìm kiếm phần tử mảng bằng np.where()
  • 2. Tìm kiếm phần tử mảng bằng np.searchsorted()

To search an array, use the where() method.

Example

Find the indexes where the value is 4:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 4, 4])

x = np.where(arr == 4)

print(x)

Try it Yourself »

The example above will return a tuple: (array([3, 5, 6],)

Which means that the value 4 is present at index 3, 5, and 6.

Example

Find the indexes where the values are even:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

x = np.where(arr%2 == 0)

print(x)

Try it Yourself »

Example

Find the indexes where the values are odd:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

x = np.where(arr%2 == 1)

print(x)

Try it Yourself »



Search Sorted

There is a method called searchsorted() which performs a binary search in the array, and returns the index where the specified value would be inserted to maintain the search order.

The searchsorted() method is assumed to be used on sorted arrays.

Example

Find the indexes where the value 7 should be inserted:

import numpy as np

arr = np.array([6, 7, 8, 9])

x = np.searchsorted(arr, 7)

print(x)

Try it Yourself »

Example explained: The number 7 should be inserted on index 1 to remain the sort order.

The method starts the search from the left and returns the first index where the number 7 is no longer larger than the next value.

Search From the Right Side

By default the left most index is returned, but we can give side='right' to return the right most index instead.

Example

Find the indexes where the value 7 should be inserted, starting from the right:

import numpy as np

arr = np.array([6, 7, 8, 9])

x = np.searchsorted(arr, 7, side='right')

print(x)

Try it Yourself »

Example explained: The number 7 should be inserted on index 2 to remain the sort order.

The method starts the search from the right and returns the first index where the number 7 is no longer less than the next value.

Multiple Values

To search for more than one value, use an array with the specified values.

Example

Find the indexes where the values 2, 4, and 6 should be inserted:

import numpy as np

arr = np.array([1, 3, 5, 7])

x = np.searchsorted(arr, [2, 4, 6])

print(x)

Try it Yourself »

The return value is an array: [1 2 3] containing the three indexes where 2, 4, 6 would be inserted in the original array to maintain the order.


Test Yourself With Exercises

Exercise:

Use the correct NumPy method to find all items with the value 4.

arr = np.array([1, 2, 3, 4, 5, 4, 4])

x = np.(arr == 4)

Start the Exercise


Tìm kiếm (Array Search) trong mảng Numpy

Nội Dung

  • 1. Tìm kiếm phần tử mảng bằng np.where()
  • 2. Tìm kiếm phần tử mảng bằng np.searchsorted()

1. Tìm kiếm phần tử mảng bằng np.where()

Trong Numpy, bạn có thể tìm kiếm phần tử trong mảng thông qua một giá trị tìm kiếm và các kết quả trả về sẽ là các chỉ mục của phần tử được tìm kiếm trong mảng.

Để tìm kiếm phần tử trong mảng Numpy, chúng ta sẽ sử dụng hàm np.where() – hàm này nhận vào một giá trị phần tử cần tìm và  nếu tìm kiếm thành công sẽ trả về chỉ mục của phần tử đó trong mảng.

Ví dụ dưới đây, tìm kiếm các phần tử có giá trị là 4 trong mảng bằng cách sử dụng hàm np.where() như sau:

import numpy as np

# Mang a
a = np.array([1, 2, 3, 4, 5, 4, 4])

# Tim cac phan tu trong mang co gia tri bang 4
x = np.where(a == 4)
print(x)

Kết quả:

(array([3, 5, 6]),)

Lưu ý: Các kết quả là các chỉ mục tìm thấy sẽ được lưu vào một mảng mới!

Chúng ta có thể kết hợp tìm kiếm phần tử dựa theo các điều kiện thông qua hàm np.where() Ví dụ dưới đây, tìm kiếm các phần tử trong mảng với điều kiện phần tử này phải chia hết cho 2 như sau:

import numpy as np

# Mang a
a = np.array([1, 2, 3, 4, 5, 4, 4])

# Tim cac phan tu trong mang chia het cho 2
x = np.where(a%2 == 0)
print(x)

Kết quả:

(array([1, 3, 5, 7]),)

2. Tìm kiếm phần tử mảng bằng np.searchsorted()

Trong Numpy, cung cấp sẵn cho chúng ta một hàm được gọi là np.searchsorted() để thực hiện tìm kiếm nhị phân trong mảng và trả về chỉ mục của phần tử khi tìm kiếm thành công.

import numpy as np

# Mang a
a = np.array([6, 7, 8, 9])

# Tim kiem phan tu co gia tri 7 bang phuong phap tim kiem nhi phan
x = np.searchsorted(a, 7)
print(x)

Kết quả:

1

Lưu ý: Hàm này sẽ không lưu các chỉ mục được tìm thấy vào một mảng, thay vào đó hàm sẽ trả về chỉ mục là một kiểu số.

Ở ví dụ trên, khi sử dụng hàm np.searchsorted() sẽ thực hiện tìm kiếm từ trái của mảng sang bên phải của mảng, vì vậy ta tìm kiếm từ trái sang phải với phần tử có giá trị 7 sẽ có vị trí là 1.

Tuy nhiên, trong một số trường hợp ta cần thực hiện tìm kiếm từ phải sang trái khi đó ta sẽ cần truyền tham số side=‘right’ vào trong hàm np.searchsorted() như sau:

import numpy as np

# Mang a
a = np.array([6, 7, 8, 9])

# Tim kiem phan tu co gia tri 7 - thuc hien tim tu ben phai sang ben trai
x = np.searchsorted(a, 7, side='right')
print(x)

Kết quả:

2

Vì từ phải sang trái, nên phần tử 9 có chỉ mục 0, phần tử 8 có chỉ mục 1, phần tử 7 có chỉ mục 2– phần tử này trùng với giá trị cần tìm kiếm, vì vậy khi tìm kiếm phần tử có giá trị 7 từ phải sang trái trong mảng ta có kết quả là 2.

Để tìm kiếm nhiều hơn một giá trị, hãy sử dụng một mảng với các giá trị và truyền vào hàm np.searchsorted() để chỉ định các phần tử cần được tìm kiếm:

import numpy as np

# Mang a
a = np.array([1, 3, 5, 7, 2, 4, 6])

# Tim kiem nhieu phan tu trong mang
x = np.searchsorted(a, [2, 4, 6])
print(x)

Kết quả:

[1 5 6]