numpy.fromiter()函数– Python
numpy.fromiter()函数从可迭代对象创建一个新的一维数组。
Syntax : numpy.fromiter(iterable, dtype, count = -1)
Parameters :
iterable : [iterable object] An iterable object providing data for the array.
dtype : [data-type] Data-type of the returned array.
count : [int, optional] Number of items to read.
Returns : [ndarray] The output array.
代码#1:
Python3
# Python program explaining
# numpy.fromiter() function
# importing numpy as geek
import numpy as geek
iterable = (x * x*x for x in range(4))
gfg = geek.fromiter(iterable, int)
print (gfg)
Python3
# Python program explaining
# numpy.fromiter() function
# importing numpy as geek
import numpy as geek
iterable = (x * x for x in range(6))
gfg = geek.fromiter(iterable, float)
print (gfg)
输出 :
[ 0 1 8 27]
代码#2:
Python3
# Python program explaining
# numpy.fromiter() function
# importing numpy as geek
import numpy as geek
iterable = (x * x for x in range(6))
gfg = geek.fromiter(iterable, float)
print (gfg)
输出 :
[ 0. 1. 4. 9. 16. 25.]