[英]How to append an array of space-separated integers input in an array in python?
[英]How can I input space separated integers in pyhton numpy array. (Like the list(map(int,input().spli(" ")) function does for a list.)
我试图找到替代方案,但仅适用于列表,不适用于 numpy 数组
我试过这个但没有用:
5
1 2 3 4 5
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "/usr/local/lib/python3.8/dist-packages/numpy/core/numeric.py", line 204, in ones
a = empty(shape, dtype, order)
TypeError: expected sequence object with len >= 0 or a single integer
我需要一个用于 numpy 数组的list(map(int,input().split(" "))
版本。
您可以转换为 numpy 数组:
import numpy as np
numbers = input('Enter some numbers: ').split()
x = np.array(list(map(int, numbers)))
print(x)
输出:
Enter some numbers: 1 2 3 4 5
[1 2 3 4 5]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.