如何为numpy数组返回以列为主的迭代器? [英] How can I return a column-major iterator for a numpy array?

查看:130
本文介绍了如何为numpy数组返回以列为主的迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ndarray对象具有flat属性(例如array.flat),该属性允许一个对象遍历其元素.例如:

ndarray objects in numpy have a flat property (e.g. array.flat) that allows one to iterate through its elements. For example:

>>> x = np.arange(1, 7).reshape(2, 3)
>>> x
array([[1, 2, 3],
       [4, 5, 6]])
>>> x.flat[3]
4

但是如何返回以列为主的1D迭代器,以便上面的示例返回5而不是4?

But how can I return a column-major 1D iterator, so that the example above returns 5 instead of 4?

推荐答案

不确定这是否是最好的方法,但似乎很简单

Not sure if this is the best way, but it seems that simply

array.T.flat

将给出我想要的结果.尽管我希望可以使用order='F'指定一些合适的方法,这样一目了然更容易理解.

will give the result I'm looking for. Although I wish there was some appropriate method that I could specify with order='F', which would be easier to understand at a glance.

这篇关于如何为numpy数组返回以列为主的迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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