a guest Jan 17th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- t = np.array([['one','two','three'],['four','five','six']], dtype=object)
- # gives 'five'
- # gives 'two'
- # 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))
- # 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
RAW Paste Data