多次元配列の場合

>>> a = [[3,2,1],[5,4,6]]
>>> [*map(lambda x: sorted(x), a)]
[[1, 2, 3], [4, 5, 6]]

普通にやるだけです。

np.arrayの場合

np.vectorizeは一次元配列の場合mapのように動きますが、多次元の場合、最小の一要素を見ていくようで、ダメです。

>>> a = np.array([[3,2,1],[5,4,6]])
>>> np.vectorize(lambda x: sorted(x))(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/numpy/1.14.3_1/lib/python3.6/site-packages/numpy/lib/function_base.py", line 2755, in __call__
    return self._vectorize_call(func=func, args=vargs)
  File "/usr/local/Cellar/numpy/1.14.3_1/lib/python3.6/site-packages/numpy/lib/function_base.py", line 2825, in _vectorize_call
    ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
  File "/usr/local/Cellar/numpy/1.14.3_1/lib/python3.6/site-packages/numpy/lib/function_base.py", line 2785, in _get_ufunc_and_otypes
    outputs = func(*inputs)
  File "<stdin>", line 1, in <lambda>
TypeError: 'numpy.int64' object is not iterable

np.apply_along_axisを使いましょう。

>>> a = np.array([[3,2,1],[5,4,6]])
>>> np.apply_along_axis(lambda x: sorted(x), 1, a)
array([[1, 2, 3],
       [4, 5, 6]])
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.