如何添加到阵列,使得匹配的元素成为自己的阵列 [英] How to add to arrays such that matching elements become their own arrays

查看:86
本文介绍了如何添加到阵列,使得匹配的元素成为自己的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得有快速的方式与numpy的做到这一点,但我似乎无法找到的功能吧。

我需要三个数组:

  A = [1,2,3]
B = [1,2,3]
C = [1,2,3]Z = np.somefunction(A,B,C)
打印ž([1,1,1],[2,2,2],[3,3,3])


解决方案

您可以使用 np.dstack

 >>> np.dstack((A,B,C))
阵列([[[1,1,1],
        [2,2,2],
        [3,3,3]]])

另外,作为说@沃伦Weckesser的评论,因为 np.dstack 表示,返回一个3D阵列作为一个更好的方式,可以使用下列方式:

 >>> np.array((A,B,C)):T
阵列([[1,1,1],
       [2,2,2],
       [3,3,3]])

I feel like there is quick way to do this with Numpy but I can't seem to find the function for it.

I need to take three arrays:

a = [1,2,3]
b = [1,2,3]
c = [1,2,3]

Z = np.somefunction(a,b,c)
print Z

([1,1,1],[2,2,2],[3,3,3])

解决方案

You can use np.dstack :

>>> np.dstack((a,b,c))
array([[[1, 1, 1],
        [2, 2, 2],
        [3, 3, 3]]])

Also as says @ Warren Weckesser said in comment since np.dstack returns a 3d array as a better way you can use following way :

>>> np.array((a, b, c)).T
array([[1, 1, 1],
       [2, 2, 2],
       [3, 3, 3]])

这篇关于如何添加到阵列,使得匹配的元素成为自己的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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