什么是&QUOT相当于;拉链()"在Python的numpy的? [英] What is the equivalent of "zip()" in Python's numpy?

查看:109
本文介绍了什么是&QUOT相当于;拉链()"在Python的numpy的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试做以下,但与numpy的数组:

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)

这应该给的结果是:

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

但是,如果输入矢量是一个numpy的阵列

But if the input vector is a numpy array:

y = np.array(x)
numpy_result = zip(*y)
print type(numpy_result)

据(果然)返回一个:

It (expectedly) returns a:

<type 'list'>

问题是,我需要在此之后将结果转换回一个numpy的阵列。

The issue is that I will need to transform the result back into a numpy array after this.

我想知道的是什么,如果有一个有效的numpy的功能,这将避免这些回来,来回变换?

推荐答案

您可以只转吧...

>>> 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. ]])

这篇关于什么是&QUOT相当于;拉链()&QUOT;在Python的numpy的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆