numpy.fromiter
-
numpy.fromiter(iterable, dtype, count=-1, *, like=None) -
Create a new 1-dimensional array from an iterable object.
- Parameters
-
-
iterableiterable object -
An iterable object providing data for the array.
-
dtypedata-type -
The data-type of the returned array.
-
countint, optional -
The number of items to read from iterable. The default is -1, which means all data is read.
-
likearray_like -
Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as
likesupports the__array_function__protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.Note
The
likekeyword is an experimental feature pending on acceptance of NEP 35.New in version 1.20.0.
-
- Returns
-
-
outndarray -
The output array.
-
Notes
Specify
countto improve performance. It allowsfromiterto pre-allocate the output array, instead of resizing it on demand.Examples
>>> iterable = (x*x for x in range(5)) >>> np.fromiter(iterable, float) array([ 0., 1., 4., 9., 16.])
© 2005–2021 NumPy Developers
Licensed under the 3-clause BSD License.
https://numpy.org/doc/1.20/reference/generated/numpy.fromiter.html