美文网首页我爱编程
Python之Numpy实践笔记(2)

Python之Numpy实践笔记(2)

作者: ankiyang | 来源:发表于2016-04-28 17:53 被阅读0次

numpy.flatiter

flat属性将返回一个numpy.flatiter对象,这个扁平迭代器可以遍历多维数组(好像是一维数组一样遍历)。

上栗子先:

In [1]: x = np.arange(6).reshape(2,3)

In [2]: f1 = x.flat

In [3]: type(f1)

Out[3]: numpy.flatiter

In [4]: f1

Out[4]:

In [5]: for item in f1: print item

0

1

2

3

4

5

可以用这个flatiter对象直接获取其中的一个或多个数组元素:

In [6]: x.flat[2]

Out[6]: 2

In [7]: x.flat[2:4]

Out[7]: array([2, 3])

同样也可赋值,将导致整个数组的所有元素都被覆盖:

In [9]: x.flat=8

In [10]: x

Out[10]:

array([[8, 8, 8],

[8, 8, 8]])

选择性赋值:

In [11]: x.flat[[1,3]]=1

In [12]: x

Out[12]:

array([[8, 1, 8],

[1, 8, 8]])

Sourece:http://docs.scipy.org/doc/numpy/reference/generated/numpy.flatiter.html

相关文章

网友评论

    本文标题:Python之Numpy实践笔记(2)

    本文链接:https://www.haomeiwen.com/subject/ymshrttx.html