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

  • Home
  • SEARCH
  • 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 7612649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:58:03+00:00 2026-05-31T01:58:03+00:00

I am looking for a numpy function to find the indices at which certain

  • 0

I am looking for a numpy function to find the indices at which certain values are found within a vector (xs). The values are given in another array (ys). The returned indices must follow the order of ys.

In code, I want to replace the list comprehension below by a numpy function.

>> import numpy as np
>> xs = np.asarray([45, 67, 32, 52, 94, 64, 21])
>> ys = np.asarray([67, 94])
>> ndx = np.asarray([np.nonzero(xs == y)[0][0] for y in ys]) # <---- This line
>> print(ndx)
[1 4]

Is there a fast way?

Thanks

  • 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-05-31T01:58:04+00:00Added an answer on May 31, 2026 at 1:58 am

    For big arrays xs and ys, you would need to change the basic approach for this to become fast. If you are fine with sorting xs, then an easy option is to use numpy.searchsorted():

    xs.sort()
    ndx = numpy.searchsorted(xs, ys)
    

    If it is important to keep the original order of xs, you can use this approach, too, but you need to remember the original indices:

    orig_indices = xs.argsort()
    ndx = orig_indices[numpy.searchsorted(xs[orig_indices], ys)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function gen() which returns a numpy array of nElements number of
I'm looking for a numpy function that will do the equivalent of: indices =
I want to retrieve k max values from each row in a numpy array.
I am looking for a function to calculate exponential moving sum in numpy or
I'm looking at some Python numpy code, which contains lines like a = 1.
For example, given a python numpy.ndarray a = array([[1, 2], [3, 4], [5, 6]])
Is it possible to map a NumPy array in place? If yes, how? Given
Given a numpy array and a __getitem__ -type index, is there an idiomatic way
Is there a numpy function to divide an array along an axis with elements
I'm looking to quickly (hopefully without a for loop) generate a Numpy array of

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.