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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:44:12+00:00 2026-06-12T16:44:12+00:00

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

  • 0

In the Python’s standard max function (I also 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, I can not longer use max, so I tried to use numpy.amax, however I can’t seem to be able to use amax with strings…

t = np.array([['one','two','three'],['four','five','six']])
t.dtype # dtype('|S5')
numpy.amax(t, axis=0) #Error! Hoping for: [`two`, `six`]

Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 1833, in amax
        return amax(axis, out)
TypeError: cannot perform reduce with flexible type

Is it possible to use amax (am using it incorrectly!), or is there some other numpy tool to do this?

  • 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-12T16:44:13+00:00Added an answer on June 12, 2026 at 4:44 pm

    Instead of storing your strings as variable length data in the numpy array, you could try storing them as Python objects instead. Numpy will treat these as references to the original Python string objects, and you can then treat them like you might expect:

    t = np.array([['one','two','three'],['four','five','six']], dtype=object)
    np.min(t)
    # gives 'five'
    np.max(t)
    # gives 'two'
    

    Keep in mind that here, the np.min and np.max calls are ordering the strings lexicographically – so “two” does indeed come after “five”. To change the comparison operator to look at the length of each string, you could try creating a new numpy array identical in form, but containing each string’s length instead of its reference. You could then do a numpy.argmin call on that array (which returns the index of the minimum) and look up the value of the string in the original array.


    Example code:

    # Vectorize takes a Python function and converts it into a Numpy
    # vector function that operates on arrays
    np_len = np.vectorize(lambda x: len(x))
    
    np_len(t)
    # gives array([[3, 3, 5], [4, 4, 3]])
    
    idx = np_len(t).argmin(0) # get the index along the 0th axis
    # gives array([0, 0, 1])
    
    result = t
    for i in idx[1:]:
        result = result[i]
    print result
    # gives "two", the string with the smallest length
    
    • 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) Given two numbers A and B. I need to find all nested groups
Python's multiprocessing.connection allows for a Listener-Client pattern . You can choose the type of
Python dict key delete, if key pattern match with other dict key. e.g. a={'a.b.c.test':1,
Python allows to call a static method not only from a class, but also
Python's arbitrary precision decimals are lovely, but I can't seem to find a way
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

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.