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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:34:47+00:00 2026-06-04T06:34:47+00:00

I want to check if a NumPyArray has values in it that are in

  • 0

I want to check if a NumPyArray has values in it that are in a set, and if so set that area in an array = 1. If not set a keepRaster = 2.

numpyArray = #some imported array
repeatSet= ([3, 5, 6, 8])

confusedRaster = numpyArray[numpy.where(numpyArray in repeatSet)]= 1

Yields:

<type 'exceptions.TypeError'>: unhashable type: 'numpy.ndarray'

Is there a way to loop through it?

 for numpyArray
      if numpyArray in repeatSet
           confusedRaster = 1
      else
           keepRaster = 2

To clarify and ask for a bit further help:

What I am trying to get at, and am currently doing, is putting a raster input into an array. I need to read values in the 2-d array and create another array based on those values. If the array value is in a set then the value will be 1. If it is not in a set then the value will be derived from another input, but I’ll say 77 for now. This is what I’m currently using. My test input has about 1500 rows and 3500 columns. It always freezes at around row 350.

for rowd in range(0, width):
    for cold in range (0, height):
        if numpyarray.item(rowd,cold) in repeatSet:
            confusedArray[rowd][cold] = 1
        else:
            if numpyarray.item(rowd,cold) == 0:
                confusedArray[rowd][cold] = 0
            else:
                confusedArray[rowd][cold] = 2
  • 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-04T06:34:48+00:00Added an answer on June 4, 2026 at 6:34 am

    In versions 1.4 and higher, numpy provides the in1d function.

    >>> test = np.array([0, 1, 2, 5, 0])
    >>> states = [0, 2]
    >>> np.in1d(test, states)
    array([ True, False,  True, False,  True], dtype=bool)
    

    You can use that as a mask for assignment.

    >>> test[np.in1d(test, states)] = 1
    >>> test
    array([1, 1, 1, 5, 1])
    

    Here are some more sophisticated uses of numpy’s indexing and assignment syntax that I think will apply to your problem. Note the use of bitwise operators to replace if-based logic:

    >>> numpy_array = numpy.arange(9).reshape((3, 3))
    >>> confused_array = numpy.arange(9).reshape((3, 3)) % 2
    >>> mask = numpy.in1d(numpy_array, repeat_set).reshape(numpy_array.shape)
    >>> mask
    array([[False, False, False],
           [ True, False,  True],
           [ True, False,  True]], dtype=bool)
    >>> ~mask
    array([[ True,  True,  True],
           [False,  True, False],
           [False,  True, False]], dtype=bool)
    >>> numpy_array == 0
    array([[ True, False, False],
           [False, False, False],
           [False, False, False]], dtype=bool)
    >>> numpy_array != 0
    array([[False,  True,  True],
           [ True,  True,  True],
           [ True,  True,  True]], dtype=bool)
    >>> confused_array[mask] = 1
    >>> confused_array[~mask & (numpy_array == 0)] = 0
    >>> confused_array[~mask & (numpy_array != 0)] = 2
    >>> confused_array
    array([[0, 2, 2],
           [1, 2, 1],
           [1, 2, 1]])
    

    Another approach would be to use numpy.where, which creates a brand new array, using values from the second argument where mask is true, and values from the third argument where mask is false. (As with assignment, the argument can be a scalar or an array of the same shape as mask.) This might be a bit more efficient than the above, and it’s certainly more terse:

    >>> numpy.where(mask, 1, numpy.where(numpy_array == 0, 0, 2))
    array([[0, 2, 2],
           [1, 2, 1],
           [1, 2, 1]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to check whether my input array is consecutive or not? In my
I want check that that provided Rss feed Link Is valid or not and
I want to check if a hidden field has a value or not and
I want to check if a particular page gets redirected or not. However, whenever
I want to check the type of an Object. How can I do that?
I want to check that a file system path is valid and safe to
I am trying to check a field in some XML that is returned from
Want to check that my site map contains a page. Could just iterate through
I want check if there is not class .myclass2 in classs .myclass1 alert ok
I want a check box or toggle button in listView, and set onclicklisterner but

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.