将2d数组拆分为每行数组的最有效方法是什么? [英] what is the most pythonic way to split a 2d array to arrays of each row?

查看:67
本文介绍了将2d数组拆分为每行数组的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数foo,它返回形状为(1000,2)的数组我如何将其拆分为两个数组a(1000)和b(1000)我正在寻找这样的东西:

I have a function foo that returns an array with the shape (1000, 2) how can I split it to two arrays a(1000) and b(1000) I'm looking for something like this:

a;b = foo()

我正在寻找一个可以轻松概括为形状为(1000,5)左右的情况的答案.

I'm looking for an answer that can easily generalize to the case in which the shape is (1000, 5) or so.

推荐答案

转置并解压缩?

a, b = foo().T


>>> a, b = np.arange(20).reshape(-1, 2).T
>>> a
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
>>> b
array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19])

这篇关于将2d数组拆分为每行数组的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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