将数组输出更改为不带括号的数组

时间:2022-03-26 02:42:45

I changed a sparse dictionary into an array with (np.asarray). Then, I wrote a function that used that array to return the answer of a formula. However, I did that in a way the output includes the double brackets. Let's say the output is now:

我用(np.asarray)将稀疏字典更改为数组。然后,我写了一个函数,使用该数组返回公式的答案。但是,我这样做的输出包括双括号。我们现在说输出是:

[[7.58939191]]

but should be:

但应该是:

7.58939191

Can someone say how I can change this easily? Or do I have to share my function for this?

有人可以说我怎么能轻易改变这个?或者我必须分享我的功能吗?

2 个解决方案

#1


1  

One way could be item method:

一种方法可能是项目方法:

x.item(0)

See the documentation:

查看文档:

Copy an element of an array to a standard Python scalar and return it.

将数组元素复制到标准Python标量并返回它。

#2


0  

You can turn it into a numpy array, then compress the dimension:

您可以将其转换为numpy数组,然后压缩维度:

import numpy as np
a = np.squeeze(np.asarray(a))

Then you can use a just like a number, for example:

然后你可以像数字一样使用,例如:

b = a + 1

#1


1  

One way could be item method:

一种方法可能是项目方法:

x.item(0)

See the documentation:

查看文档:

Copy an element of an array to a standard Python scalar and return it.

将数组元素复制到标准Python标量并返回它。

#2


0  

You can turn it into a numpy array, then compress the dimension:

您可以将其转换为numpy数组,然后压缩维度:

import numpy as np
a = np.squeeze(np.asarray(a))

Then you can use a just like a number, for example:

然后你可以像数字一样使用,例如:

b = a + 1