繁体   English   中英

如何在 pyhton numpy 数组中输入空格分隔的整数。 (就像 list(map(int,input().spli(" ")) 函数对列表所做的那样。)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM