numpy.fromstring()函数– Python
numpy.fromstring()函数创建一个从字符串中的文本数据初始化的新一维数组。
Syntax : numpy.fromstring(string, dtype = float, count = -1, sep = ‘ ‘)
Parameters :
string : [str] A string that contained the data.
dtype : [data-type, optional] Data-type of the array. Default data type is float.
count : [int, optional] Number of items to read. If this is negative (the default), the count will be determined from the length of the data.
sep : [str, optional] The string separating numbers in the data.
Return : [ndarray] Return the constructed array.
代码#1:
Python3
# Python program explaining
# numpy.fromstring() function
# importing numpy as geek
import numpy as geek
gfg = geek.fromstring('1 2 3 4 5', dtype = float, sep = ' ')
print(gfg)
Python3
# Python program explaining
# numpy.fromstring() function
# importing numpy as geek
import numpy as geek
gfg = geek.fromstring('1 2 3 4 5 6', dtype = int, sep = ' ')
print(gfg)
输出 :
[1. 2. 3. 4. 5.]
代码#2:
Python3
# Python program explaining
# numpy.fromstring() function
# importing numpy as geek
import numpy as geek
gfg = geek.fromstring('1 2 3 4 5 6', dtype = int, sep = ' ')
print(gfg)
输出 :
[1 2 3 4 5 6]