What is the equivalent of “zip()” in Python's numpy?

前端 未结 2 716
灰色年华
灰色年华 2020-12-25 09:39

I am trying to do the following but with numpy arrays:

x = [(0.1, 1.), (0.1, 2.), (0.1, 3.), (0.1, 4.), (0.1, 5.)]
normal_result = zip(*x)

2条回答
  •  旧时难觅i
    2020-12-25 09:53

    You can just transpose it...

    >>> a = np.array([(0.1, 1.), (0.1, 2.), (0.1, 3.), (0.1, 4.), (0.1, 5.)])
    >>> a
    array([[ 0.1,  1. ],
           [ 0.1,  2. ],
           [ 0.1,  3. ],
           [ 0.1,  4. ],
           [ 0.1,  5. ]])
    >>> a.T
    array([[ 0.1,  0.1,  0.1,  0.1,  0.1],
           [ 1. ,  2. ,  3. ,  4. ,  5. ]])
    

提交回复
热议问题