简体   繁体   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.)

I have tried to find alternatives but only available for list not for numpy arrays我试图找到替代方案,但仅适用于列表,不适用于 numpy 数组

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.我需要一个用于 numpy 数组的list(map(int,input().split(" "))版本。

You can just convert to a numpy array:您可以转换为 numpy 数组:

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 的数组中输入一个空格分隔的整数数组 append? - How to append an array of space-separated integers input in an array in python? 如何从文本文件中提取由空格分隔的数字列表并将它们转换为整数数组。 - Python - How do I extract a list of numbers separated by a space from a text file and convert them to an integer array. - Python 如何让用户只输入 5 个整数/字符串 a 到列表中? - How can I have the user input only 5 integers/strings a into a list? 我如何管理 pyhton 中的 input()? - how can i manage the input() in pyhton? 使用 map() function 的整数列表输入的绝对值 - Absolute vaule of an input of a list of integers using map() function 如何将 python 中的 n 个整数列表更改为普通空格分隔的整数 - how to change a list of n integers in python to normal space separated integers 将python列表复制到numpy数组时,如何防止TypeError:列表索引必须是整数,而不是元组? - How can I prevent the TypeError: list indices must be integers, not tuple when copying a python list to a numpy array? function 如何处理不是逗号分隔的 NumPy 数组输入? - How can a function handle a NumPy array input, which is not comma-separated? 如何检查 int(input()) 是否在列表长度的范围内? - How can I check if an int(input()) is in the range of the length of a list? 如何使用Python 3中的readlines读取由空格分隔的整数输入文件? - How to read an input file of integers separated by a space using readlines in Python 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM