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 6806175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:40:41+00:00 2026-05-26T19:40:41+00:00

It seems I still struggle with the in operator in numpy . Here’s the

  • 0

It seems I still struggle with the “in” operator in numpy. Here’s the situation:

>>> a = np.random.randint(1, 10, (2, 2, 3))
>>> a
array([[[9, 8, 8],
        [4, 9, 1]],

       [[6, 6, 3],
        [9, 3, 5]]])

I would like to get the indexes of those triplets whose second element is in (6, 8). The way I intuitively tried is:

>>> a[:, :, 1] in (6, 8)
ValueError: The truth value of an array with more than one element...

My ultimate goal would be to insert at those positions the the number preceding those multiplied by two. Using the example above, a should become:

array([[[9, 18, 8],   #8 @ pos #2 --> replaced by 9 @ pos #1 by 2
        [4, 9, 1]],

       [[6, 12, 3],   #6 @ pos #2 --> replaced by 6 @ pos #1 by 2
        [9, 3, 5]]])

Thank you in advance for your advice and time!

  • 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-26T19:40:42+00:00Added an answer on May 26, 2026 at 7:40 pm
    import numpy as np
    a = np.array([[[9, 8, 8],
                   [4, 9, 1]],
    
                  [[6, 6, 3],
                   [9, 3, 5]]])
    
    ind=(a[:,:,1]<=8) & (a[:,:,1]>=6)
    a[ind,1]=a[ind,0]*2
    print(a)
    

    yields

    [[[ 9 18  8]
      [ 4  9  1]]
    
     [[ 6 12  3]
      [ 9  3  5]]]
    

    If you wish to check for membership in a set which is not a simple range, then I like both mac’s idea of using a Python loop and bellamyj’s idea of using np.in1d. Which is faster depends on the size of check_tuple:

    test.py:

    import numpy as np
    np.random.seed(1)
    
    N = 10
    a = np.random.randint(1, 1000, (2, 2, 3))
    check_tuple = np.random.randint(1, 1000, N)
    
    def using_in1d(a):
        idx = np.in1d(a[:,:,1], check_tuple)
        idx=idx.reshape(a[:,:,1].shape)
        a[idx,1] = a[idx,0] * 2
        return a
    
    def using_in(a):
        idx = np.zeros(a[:,:,0].shape,dtype=bool)
        for n in check_tuple:
            idx |= a[:,:,1]==n
        a[idx,1] = a[idx,0]*2
        return a
    
    assert np.allclose(using_in1d(a),using_in(a))    
    

    When N = 10, using_in is slightly faster:

    % python -m timeit -s'import test' 'test.using_in1d(test.a)'
    10000 loops, best of 3: 156 usec per loop
    % python -m timeit -s'import test' 'test.using_in(test.a)'
    10000 loops, best of 3: 143 usec per loop
    

    When N = 100, using_in1d is much faster:

    % python -m timeit -s'import test' 'test.using_in1d(test.a)'
    10000 loops, best of 3: 171 usec per loop
    % python -m timeit -s'import test' 'test.using_in(test.a)'
    1000 loops, best of 3: 1.15 msec per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a basic question, but it's still bugging me. Why doesn't MyObject
Seems like everyone always ignores the time part, but how would you compare two
Sigh - Rails 3 routing still seems to confound me. Here's the route.rb line
I am releasing things but the memory seems to still be there according to
I have read a lot about this issue but mine still seems to be
At present it seems that VS2008 still isn't supported either in the 5.1.5 release
I can't believe that the following statement seems to be still true So, I
This question seems to be asked freqeuently over the internet but I still can't
I'm still working on a particular RDF file, but seems that something is not
I am still getting started with Unity, and have what seems to be 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.