简体   繁体   中英

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

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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