I would like to concatenate an array including different element sizes python as an user input unfortunately it did not work with user input, I got the following error "Value error: Zero-dimensional arrays can not be concatenated" I do not understand how it works normally but with user input do not work ?
import numpy as np
#lst_2=np.concatenate([[1],[2],np.repeat(3,3),[2]])
lst_2=input("PLEASE ENTER THE THING:: ")
print('OKKKKL',lst_2)
np.concatenate(lst_2)
CodePudding user response:
the input()
function always returns a string. If your input is like 1234
, you have to write: lst_2=input("PLEASE ENTER THE THING:: ").split('')
CodePudding user response:
OK, in more detail, you create a list:
In [251]: alist = [[1],[2],np.repeat(3,3),[2]]
In [252]: alist
Out[252]: [[1], [2], array([3, 3, 3]), [2]]
and apply concatenate
to that list:
In [253]: np.concatenate(alist)
Out[253]: array([1, 2, 3, 3, 3, 2])
With input
, if I copy-n-paste the same thing, I get a string
:
In [254]: astr = input()
[[1],[2],np.repeat(3,3),[2]]
In [255]: astr
Out[255]: '[[1],[2],np.repeat(3,3),[2]]'
The quotes are important. concatenate
does not work with that:
In [256]: np.concatenate(astr)
Traceback (most recent call last):
File "<ipython-input-256-95017eec7f88>", line 1, in <module>
np.concatenate(astr)
File "<__array_function__ internals>", line 5, in concatenate
ValueError: zero-dimensional arrays cannot be concatenated
The string has to be converted to a list first:
In [257]: alist1 = eval(astr) # better ast.literal_eval
In [258]: alist1
Out[258]: [[1], [2], array([3, 3, 3]), [2]]
In [259]: np.concatenate(alist1)
Out[259]: array([1, 2, 3, 3, 3, 2])
Entering strings as code is different from entering them via the input
function.
CodePudding user response:
First of all the input will be taken as a string. You need to take that string and convert it to a list. This is achieved by splitting it with respect to a separator. Finally just use the np.array
to create an array from a list. np.concatenate
is used to put together objects that are already arrays.
import numpy as np
raw_input=input("PLEASE ENTER THE THING:: ")
# here i use space, but anything can be the separator
input_list = raw_input.split(" ")
# dtype is optional, but unless you specify it, it will be `string`
arr = np.array(input_list, dtype=int)
UPDATE:
If you want repeating elements, as you said, you could give pairs of numbers, as inputs, like this:
import numpy as np
user_input = input("PLEASE ENTER THE THING:: ")
input_list = user_input.split(",")
print(input_list)
arr = np.concatenate(
[np.repeat(int(pair.split(' ')[0]),
int(pair.split(' ')[1]))
for pair in input_list]
)
print(arr)
# example input: 1 10,2 3,4 4
# output [1 1 1 1 1 1 1 1 1 1 2 2 2 4 4 4 4]
here you have pairs separated by "," and the numbers in each pair are separated by a space. This script will repeat the first number in the pair as many times as the second number specifies. In order for this to work make sure you don't add a space after the comma when you input numbers.