numpy的:访问由列数组 [英] numpy: access an array by column

查看:98
本文介绍了numpy的:访问由列数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有:

 检测= numpy.array([1,2],[3,4],[5,6]])

测试[I] 让我的第i 的数组中的线(例如 [1,2] )。我怎样才能访问的第i 的列? (例如: [1,3,5] )。此外,这会是一个昂贵的操作?


解决方案

 >>>测试[:,0]
阵列([1,3,5])

同样,

 >>>测试[1 ,:]
阵列([3,4])

您可以访问行。这是包括在 numpy的参考的第1.4(索引)。这是快速的,在我的经验最少。这肯定比在一个循环访问每个元素快得多。

Suppose I have:

test = numpy.array([[1, 2], [3, 4], [5, 6]])

test[i] gets me ith line of the array (eg [1, 2]). How can I access the ith column? (eg [1, 3, 5]). Also, would this be an expensive operation?

解决方案

>>> test[:,0]
array([1, 3, 5])

Similarly,

>>> test[1,:]
array([3, 4])

lets you access rows. This is covered in Section 1.4 (Indexing) of the NumPy reference. This is quick, at least in my experience. It's certainly much quicker than accessing each element in a loop.

这篇关于numpy的:访问由列数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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