Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8360803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:33:12+00:00 2026-06-09T11:33:12+00:00

Let’s say that I have two arrays in the form a = [0, 0,

  • 0

Let’s say that I have two arrays in the form

a = [0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 6]
b = [1, 2, 1, 2, 1, 4, 7, 9, 4, 8, 1, 1]

As you can see, the above arrays are sorted, when considered a and b as columns of a super array.

Now, I want to do a searchsorted on this array. For instance, if I search for (3, 7) (a = 3 and b = 7), I should get 6.

Whenever there are duplicate values in a, the search should continue with values in b.

Is there a built-in numpy method to do it? Or what could be the efficient way to do it, assuming that I have million entries in my array.

I tried with numpy.recarray, to create one recarray with a and b and tried searching in it, but I am getting the following error.

TypeError: expected a readable buffer object

Any help is much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:33:13+00:00Added an answer on June 9, 2026 at 11:33 am

    You’re almost there. It’s just that numpy.record (which is what I assume you used, given the error message you received) isn’t really what you want; just create a one-item record array:

    >>> a_b = numpy.rec.fromarrays((a, b))
    >>> a_b
    rec.array([(0, 1), (0, 2), (1, 1), (1, 2), (2, 1), (3, 4), (3, 7), (3, 9),
           (4, 4), (4, 8), (5, 1), (6, 1)], 
          dtype=[('f0', '<i8'), ('f1', '<i8')])
    >>> numpy.searchsorted(a_b, numpy.array((3, 7), dtype=a_b.dtype))
    6
    

    It might also be useful to know that sort and argsort sort record arrays lexically, and there is also lexsort. An example using lexsort:

    >>> random_idx = numpy.random.permutation(range(12))
    >>> a = numpy.array(a)[random_idx]
    >>> b = numpy.array(b)[random_idx]
    >>> sorted_idx = numpy.lexsort((b, a))
    >>> a[sorted_idx]
    array([0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 6])
    >>> b[sorted_idx]
    array([1, 2, 1, 2, 1, 4, 7, 9, 4, 8, 1, 1])
    

    Sorting record arrays:

    >>> a_b = numpy.rec.fromarrays((a, b))
    >>> a_b[a_b.argsort()]
    rec.array([(0, 1), (0, 2), (1, 1), (1, 2), (2, 1), (3, 4), (3, 7), (3, 9),
           (4, 4), (4, 8), (5, 1), (6, 1)], 
          dtype=[('f0', '<i8'), ('f1', '<i8')])
    >>> a_b.sort()
    >>> a_b
    rec.array([(0, 1), (0, 2), (1, 1), (1, 2), (2, 1), (3, 4), (3, 7), (3, 9),
           (4, 4), (4, 8), (5, 1), (6, 1)], 
          dtype=[('f0', '<i8'), ('f1', '<i8')])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say we have a table that has 2 million rows. It has two
Let's say I have a table with a Color column. Color can have various
Let's say that I have a SQLite database that I create in a separate
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let assume we have two activities. A - main activity, that is home launcher
Let's say you have a method that expects a numerical value as an argument.
Let's say I have a bunch of links that share a click event: <a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.