填写np.array时出错

时间:2018-05-21 05:07:08

标签: python numpy

我正在尝试使用for循环填充数组,但结果似乎总是最后输入的数字

import numpy as np
quantity = int (input ("enter amount of data"))
for i in range (quantity):
         value = float (input ("enter value of x >>"))
         x = np.array ([value])

print (x)

1 个答案:

答案 0 :(得分:1)

您需要声明数组一次

quantity = int(input("enter amount of data"))
arr = np.zeros(quantity, dtype=float)

然后根据需要填写单个元素:

for i in range(quantity):
    arr[i] = float(input("enter value of x >>"))