[英]Creating an array with user input in python
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 ?我想连接一个包含不同元素大小的数组 python 作为用户输入不幸的是它不适用于用户输入,我收到以下错误“值错误:零维数组无法连接”我不明白它是如何工作的通常但用户输入不起作用?
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)
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.array
从列表创建一个数组。 np.concatenate
is used to put together objects that are already arrays. np.concatenate
用于将已经是数组的对象放在一起。
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)
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.
为了使其工作,请确保在输入数字时不要在逗号后添加空格。
the input()
function always returns a string. input()
函数总是返回一个字符串。 If your input is like 1234
, you have to write: lst_2=input("PLEASE ENTER THE THING:: ").split('')
如果你的输入是
1234
,你必须写: lst_2=input("PLEASE ENTER THE THING:: ").split('')
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:并将
concatenate
应用于该列表:
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
:使用
input
,如果我复制粘贴同样的东西,我会得到一个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: concatenate
不适用于:
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.以代码形式输入字符串与通过
input
函数输入字符串不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.