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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:45:34+00:00 2026-06-12T01:45:34+00:00

In the Python’s standard max function I can pass in a key parameter: s

  • 0

In the Python’s standard max function I can pass in a key parameter:

s = numpy.array(['one','two','three'])
max(s) # 'two' (lexicographically last)
max(s, key=len) # 'three' (longest string)

With a larger (multi-dimensional) array, we can not longer use max, but we can use numpy.amax… which unfortunately offers no key parameter.

t = numpy.array([['one','two','three'],
                 ['four','five','six']], 
                dtype='object')
numpy.amax(t) # 'two` (max of the flat array)
numpy.amax(t, axis=1) # array([two, six], dtype=object) (max of first row, followed by max of second row)

What I want to be able to do is:

amax2(t, key=len) # 'three'
amax2(t, key=len, axis=1) # array([three, four], dtype=object)

Is there a built-in method to do this?

Note: In trying to write this question the first time I couldn’t get amax working in this toy example!

  • 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-12T01:45:35+00:00Added an answer on June 12, 2026 at 1:45 am

    This is a non built-in way (it’s missing the out and keepdim parameters of features of amax when using key), it seems rather long:

    def amax2(x, *args, **kwargs):
        if 'key' not in kwargs:
            return numpy.amax(x,*args,**kwargs)
        else:
            key = kwargs.pop('key') # e.g. len, pop so no TypeError: unexpected keyword
            x_key = numpy.vectorize(key)(x) # apply key to x element-wise
            axis = kwargs.get('axis') # either None or axis is set in kwargs
            if len(args)>=2: # axis is set in args
                axis = args[1]
    
            # The following is kept verbose, but could be made more efficient/shorter    
            if axis is None: # max of flattened
                max_flat_index = numpy.argmax(x_key, axis=axis)
                max_tuple_index = numpy.unravel_index(max_flat_index, x.shape)
                return x[max_tuple_index]
            elif axis == 0: # max in each column
                max_indices = numpy.argmax(x_key, axis=axis)
                return numpy.array(
                     [ x[max_i, i] # reorder for col
                         for i, max_i in enumerate(max_indices) ], 
                     dtype=x.dtype)
            elif axis == 1: # max in each row
                max_indices = numpy.argmax(x_key, axis=axis)
                return numpy.array(
                     [ x[i, max_i]
                         for i, max_i in enumerate(max_indices) ],
                     dtype=x.dtype)
    

    The idea for this function is extended from the second part of @PeterSobot’s answer to my previous question.

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

Sidebar

Related Questions

Python 2.7's argparse gives you two extension points where you can control how your
Python's logging functions allow you to pass them multiple arguments that they can interpolate
(Python) Given two numbers A and B. I need to find all nested groups
Python's arbitrary precision decimals are lovely, but I can't seem to find a way
Python is pretty clean, and I can code neat apps quickly. But I notice
Python 2 and 3, are the bytecode (pyo & pyc) backward compatible? can i
Python's file.read() function won't read anything. It always returns '' no matter what's inside
Python works on multiple platforms and can be used for desktop and web applications,
Python's IDLE has 'Check Module' (Alt-X) to check the syntax which can be called
Python's module 'random' has a function random.choice random.choice(seq) Return a random element from the

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.