1

Dear all: if I have an array, let's say

a=np.asarray([[1,1,2],[2,3,1]])

is there any way to find the point with the greatest third element. For example, in this case, I want the code to return the second point which is [1,1,2].

Then what about the greatest second element which should return [2,3,1]

thanks a lot for your great help and advice.

3
  • 3
    max(a, key=lambda e:e[2])? Commented Sep 21, 2020 at 4:20
  • @Chris, sorry for my wording. the first point is [1,1,2] and the second point is [2,3,1]. so the greatest third element is in point 1 which is 2 Commented Sep 21, 2020 at 4:20
  • Thanks a lot, @Green Cloak Guy, it worked Commented Sep 21, 2020 at 4:25

1 Answer 1

1

A more numpythonic (than in one of comments) way to get the answer, for any element of interest, is:

elemNo = 1
a[np.argmax(a[:, elemNo])]

Note that:

  • a[:, elemNo] retrieves the column of interest,
  • np.argmax(...) finds the index of the max value in this column.

So a[...] retrieves the whole row with just this index.

This solution should also work faster than map / lambda, what can be important especially for bigger arrays.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.