📜  Python中的 numpy.ndarray.flat()

📅  最后修改于: 2022-05-13 01:54:37.783000             🧑  作者: Mango

Python中的 numpy.ndarray.flat()

numpy.ndarray.flat()函数用作 N 维数组的 1_D 迭代器。
它不是 Python 内置迭代器对象的子类,否则它是numpy.flatiter实例。
句法 :

numpy.ndarray.flat()

参数 :

index : [tuple(int)] index of the values to iterate

返回 :

1-D iteration of array

代码 1:处理二维数组

Python
# Python Program illustrating
# working of ndarray.flat()
 
import numpy as geek
 
# Working on 1D iteration of 2D array
array = geek.arange(15).reshape(3, 5)
print("2D array : \n",array )
 
# Using flat() : 1D iterator over range
print("\nUsing Array : ", array.flat[2:6])
 
# Using flat() to Print 1D represented array
print("\n1D representation of array : \n ->", array.flat[0:15])


Python
# Python Program illustrating
# working of ndarray.flat()
 
import numpy as geek
 
# Working on 1D iteration of 2D array
array = geek.arange(15).reshape(3, 5)
print("2D array : \n",array )
 
# All elements set to 1
array.flat = 1
print("\nAll Values set to 1 : \n", array)
 
array.flat[3:6] = 8
array.flat[8:10] = 9
print("Changing values in a range : \n", array)


Python
# Python Program illustrating
# working of ndarray.flat()
 
import numpy as geek
 
# Working on 1D iteration of 2D array
array = geek.arange(15).reshape(3, 5)
print("2D array : \n",array )
 
print("\nID array : \n", array.flat[0:15])        
 
print("\nType of array,flat() : ", type(array.flat))
 
for i in array.flat:
    print(i, end = ' ')


输出 :

2D array : 
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]

Using Array :  [2 3 4 5]

1D representation of array : 
 -> [ 0  1  2 ..., 12 13 14]

代码 2:更改数组的值

Python

# Python Program illustrating
# working of ndarray.flat()
 
import numpy as geek
 
# Working on 1D iteration of 2D array
array = geek.arange(15).reshape(3, 5)
print("2D array : \n",array )
 
# All elements set to 1
array.flat = 1
print("\nAll Values set to 1 : \n", array)
 
array.flat[3:6] = 8
array.flat[8:10] = 9
print("Changing values in a range : \n", array)   

输出 :

2D array : 
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]

All Values set to 1 : 
 [[1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]]

Changing values in a range : 
 [[1 1 1 8 8]
 [8 1 1 9 9]
 [1 1 1 1 1]]

实际上 numpy.flatiter 是什么?
x.flat 为任何数组 x 返回一个 flatiter 迭代器。它允许在 for 循环中或通过调用其 next 方法对 N 维数组进行迭代(以行为主的方式)。
代码 3:numpy.flatitter() 的作用

Python

# Python Program illustrating
# working of ndarray.flat()
 
import numpy as geek
 
# Working on 1D iteration of 2D array
array = geek.arange(15).reshape(3, 5)
print("2D array : \n",array )
 
print("\nID array : \n", array.flat[0:15])        
 
print("\nType of array,flat() : ", type(array.flat))
 
for i in array.flat:
    print(i, end = ' ')

输出 :

2D array : 
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]

ID array : 
 [ 0  1  2 ..., 12 13 14]

Type of array,flat() :  
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 

参考 :
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flat.html#numpy.ndarray.flat
笔记 :
这些代码不会在在线 IDE 上运行。因此,请在您的系统上运行它们以探索其工作原理。