我如何在pyhton numpy阵列中输入空间分离整数。 (例如列表(Map(int,input()。spli(; quot; quot; quot;))函数适用于列表。)

发布于 2025-02-10 04:48:53 字数 453 浏览 3 评论 0原文

我试图找到替代方案,但仅适用于列表,而不是用于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

我需要一个版本的list(map(int,input()。split()。split(“”))>对于Numpy数组。

I have tried to find alternatives but only available for list not for numpy arrays

I tried this but didnt work:

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

I need a version of list(map(int,input().split(" ")) for numpy arrays.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2025-02-17 04:48:53

您只需转换为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]

You can just convert to a numpy array:

import numpy as np

numbers = input('Enter some numbers: ').split()
x = np.array(list(map(int, numbers)))
print(x)

Output:

Enter some numbers: 1 2 3 4 5
[1 2 3 4 5]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文