Python中的 numpy.asscalar()
numpy.asscalar()
函数用于将大小为 1 的数组转换为其等效的标量。
Syntax : numpy.asscalar(arr)
Parameters :
arr : [ndarray] Input array of size 1.
Return : Scalar representation of arr. The output data type is the same type returned by the input’s item method.
代码#1:工作
# Python program explaining
# numpy.asscalar() function
import numpy as geek
# creating a array of size 1
in_arr = geek.array([ 8 ])
print ("Input array : ", in_arr)
out_scalar = geek.asscalar(in_arr)
print ("output scalar from input array : ", out_scalar)
输出 :
Input array : [8]
output scalar from input array : 8
代码#2:
# Python program explaining
# numpy.asscalar() function
import numpy as geek
in_list = [2 ]
# changing the list to size 1 array
arr = geek.array(in_list)
print ("Input array from list : ", arr)
# changing the array to scalar
scalar = geek.asscalar(arr)
print ("output scalar from input list : ", scalar)
输出 :
Input array from list : [2]
output scalar from input list : 2