check if number is between row of numpy array

2024/11/23 20:18:40

Want to check if value is between the row of array. here the value 347 is in between the 1st row of aa but not second , so how to check for it ?

aa= np.array([[348 ,345],[460 , 459 ]])value = 347print(value> aa[:,0]) ==  (value < aa[:,1]) ## help here

answer must be

[True False]
Answer

You need to use != or ^ to get [True False].

But your question is asking for something completely different from the answer you are trying to get.

First, note that aa[:,0] is [348, 460] and a[:,1] is [345, 459]. So to have anything True, I think you to use a[0,:] and a[1,:] (note transposition of : with 0 or 1 -- order matters).

Now with this transposition we have that value > aa[0,:] is [False, True] and not [True, False] and value < aa[:,1] is [True True]. Your first answer should be False since 347 is not between 348 and 460 and your second should be True.

So to put it all together:

((value > aa[0,:])) == (value < aa[1,:])
#array([False,  True])

UPDATE based on your comment to your question above

I just saw your comment and I think I misunderstood what you are trying to do. Sorry I had misunderstood, but it speaks also to the importance of clearly explaining what you are trying to do in your quesiton.

If I now understand correctly, the problem is that the numbers in your row are not necessarily in the right order, so you need to check for min and max values. You can use the axis keyword in np.aminand np.amax, which you can call as np.amin(aa, axis = 1) or aa.min(axis = 1) (same for max). So your answer could look something like:

(value > aa.min(axis = 1)) == (value < aa.max(axis = 1))
#array([ True, False])
https://en.xdnf.cn/q/120612.html

Related Q&A

Print a word diagonally? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Click a button using Selenium and Python

I have the following code: <a class="sectionname" href="#" onclick="expandAll();return false;">Expand all</a> When I click on expand all, the whole page loads…

how to do Json format [duplicate]

This question already has answers here:How to store ner result in json/ database(2 answers)Closed 8 years ago.(S(PERSON Rami/NNP Eid/NNP)is/VBZstudying/VBGat/IN(ORGANIZATION Stony/NNP Brook/NNP Univers…

python numpy arange dtpye? why converting to integer was zero

x = np.arange(0.3, 12.5, 0.6)print(x)[ 0.3 0.9 1.5 2.1 2.7 3.3 3.9 4.5 5.1 5.7 6.3 6.9 7.5 8.1 8.7 9.3 9.9 10.5 11.1 11.7 12.3]x = np.arange(0.3, 12.5, 0.6,int)print…

Where is my syntax error?

Im trying to see where a Python syntax error would be hiding. Both Django and pylint claim a syntax error at custom.py:41. Lines 41-42 read:(reading_threshold =int(request.POST[reading_threshold]))I do…

programming challenge: how does this algorithm (tied to Number Theory) work?

In order to work on my python skills, I am sometimes doing various challenges on the internet (eg on hackerrank). Googling for something else, I found this problem, and the accompanying solution on the…

Running total for list of dict

Have a python list of dict as following:Dict1 = [{date: 1, name: xyz, qty: 100},{date: 1, name: xyz, qty: 200},{date: 1, name: xyz, qty: 300},{date: 1, name: xyz2, qty: 30},{date: 2, name: xyz, qty: 10…

How to convert CSV file to a specific JSON format with nested objects?

I want to populate my json message with data from a CSV file. I want each row to be a "new" json object. I am using Python and will connect the the code to an API once done. Some of the data …

How do I repeat the program? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

How to parse json data in Python? [duplicate]

This question already has answers here:How can I parse (read) and use JSON in Python?(5 answers)Closed 10 years ago.Please help me to parse this json in python.{ "IT" : [ { "firstName…