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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:15:01+00:00 2026-06-15T21:15:01+00:00

I am using a numpy array to hold a list of ordered pairs (representing

  • 0

I am using a numpy array to hold a list of ordered pairs (representing grid coordinates). The algorithm I am writing needs to check if a newly generated ordered pair is already in this array. Below is a schematic of the code:

cluster=np.array([[x1,y1]])
cluster=np.append(cluster,[[x2,y2]],axis=0)
cluster=np.append...etc.

new_spin=np.array([[x,y]])

if new_spin in cluster==False:
    do something

The problem with this current code is that it gives false positives. If x or y appear in the cluster, then new_spin in cluster evaluates as true. At first I thought a simple fix would be to ask if x and y appear in cluster, but this would not ensure that they appear as an ordered pair. To make sure they appear as an ordered pair I’d have to find the indices where x and y appear in cluster and compare them, which seems very clunky and inelegant, and I’m certain there must be a better solution out there. However, I have not been able to work it out myself.

Thanks for any help.

  • 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-15T21:15:02+00:00Added an answer on June 15, 2026 at 9:15 pm

    Let’s work through an example:

    In [7]: import numpy as np
    In [8]: cluster = np.random.randint(10, size = (5,2))
    In [9]: cluster
    Out[9]: 
    array([[9, 7],
           [7, 2],
           [8, 9],
           [1, 3],
           [3, 4]])
    
    In [10]: new_spin = np.array([[1,2]])
    
    In [11]: new_spin == cluster
    Out[11]: 
    array([[False, False],
           [False,  True],
           [False, False],
           [ True, False],
           [False, False]], dtype=bool)
    

    new_spin == cluster is a numpy array of dtype bool. It is True where the value in cluster equals the corresponding value in new_spin.

    For new_spin to be “in” cluster, a row of the above boolean array must all be True. We can find such rows by calling the all(axis = 1) method:

    In [12]: (new_spin == cluster).all(axis = 1)
    Out[12]: array([False, False, False, False, False], dtype=bool)
    

    So new_spin is “in” cluster, if any of the rows is all True:

    In [13]: 
    In [14]: (new_spin == cluster).all(axis = 1).any()
    Out[14]: False
    

    By the way, np.append is a very slow operation — slower than Python list.append. Chances are, you will get much better performance if you avoid np.append. If cluster is not too large, you may be better off making cluster a Python list of lists — at least until you are done appending items. Then, if needed, convert cluster to a numpy array with cluster = np.array(cluster).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an efficient way to resample a numpy array using zero-order hold? Ideally
I'm using numpy to create a cube array with sides of length 100, thus
I have an numpy array which I save to a image using savefig(). Then
How can I create a huge numpy array using pytables. I tried this but
I am trying to convert a PIL image into an array using NumPy. I
What is the most efficient way of serializing a numpy array using simplejson?
I am trying to convert a numpy array into a Java-like array using JPype's
When I load an array using numpy.loadtxt, it seems to take too much memory.
I have a list of values and a 1-d numpy array, and I would
I am using numpy.loadtxt to extract a large array of data from a text

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.