np.fromiter(iterable, dtype, count = -1)
从一个循环对象中提取数字,产生新的数组
- Iterable: 为生成数组提供数据的对象
- dtype: 生成的数组內数据类型
Parameters
iterable: iterable object
An iterable object providing data for the array.
dtype: data-type
The data-type of the returned array.
count: int, optional
The number of items to read from iterable. The default is -1, which means all data is read.
Returns:
out: ndarray
The output array.
np.fromiter()一般和for循环一起使用:
例一:
>>> iterable = (x*x for x in range(5))
>>> np.fromiter(iterable, float)
array([ 0., 1., 4., 9., 16.])
例二:
创建一个可以生成10个整数的生成器函数,并利用其建立一个数组
def gen():
for i in range(10):
yield i
z = np.fromiter(gen(), dtype=float, count=-1)
z