SHARE
TWEET

Untitled

a guest Jan 17th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. t = np.array([['one','two','three'],['four','five','six']], dtype=object)
  2. np.min(t)
  3. # gives 'five'
  4. np.max(t)
  5. # gives 'two'
  6.    
  7. # Vectorize takes a Python function and converts it into a Numpy
  8. # vector function that operates on arrays
  9. np_len = np.vectorize(lambda x: len(x))
  10.  
  11. np_len(t)
  12. # gives array([[3, 3, 5], [4, 4, 3]])
  13.  
  14. idx = np_len(t).argmin(0) # get the index along the 0th axis
  15. # gives array([0, 0, 1])
  16.  
  17. result = t
  18. for i in idx[1:]:
  19.     result = result[i]
  20. print result
  21. # gives "two", the string with the smallest length
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top