【问题标题】:How convert [x] into x?如何将 [x] 转换为 x?
【发布时间】:2016-09-13 15:24:45
【问题描述】:

我想知道如何将我的输出 [54] 转换为 54,这里是我的代码:

list_p=[]
list_aire=[0,0,1,2,3,1,1,1,0,0]


puissance=[list_aire[1:8]]
simpsaire=(scipy.integrate.simps(puissance,dx=6))
simpsaire=simpsaire.tolist()
simpsaire = map(int, simpsaire)
#u=";"
#str(u.join(simpsaire))
list_p.append(simpsaire)

#j = list_p.astype(np.int)

解决方案可能很明显,您可以看到我之前尝试过的其他解决方案(代码中带有#),但我找不到解决方案,所以如何,当我这样做时:

print simpsaire

得到:

54 and not [54]

用辛普森的方法?

感谢您的帮助,对不起我的英语。

【问题讨论】:

  • simpsaire = simpsaire[0] 没有帮助?
  • Erf,我一直没有将 simpsaire 视为列表,我尝试转换它,但我错了,请帮忙。

标签: list python-2.7 scipy output


【解决方案1】:

为什么要加括号?比较这些计算:

In [442]: from scipy import integrate

In [443]: integrate.simps([0,0,1,2,3,1,1,1,0,0],dx=6)
Out[443]: 54.0

额外的括号将输入转换为二维数组,因此积分现在是一个数组 - 输入的每行一个元素:

In [446]: x=integrate.simps([[0,0,1,2,3,1,1,1,0,0][1:8]],dx=6)

In [447]: x
Out[447]: array([ 54.])

看看你的表达会产生什么

In [448]: x.tolist()
Out[448]: [54.0]

In [449]: list(map(int,x.tolist()))
Out[449]: [54]

有更简单的方法来做到这一点

In [450]: int(x)
Out[450]: 54

In [451]: x[0]
Out[451]: 54.0

In [452]: x.item()
Out[452]: 54.0

【讨论】:

  • 感谢您的帮助!我现在明白我的错误了,你!
猜你喜欢
  • 1970-01-01
  • 2012-03-06
  • 2010-12-26
  • 1970-01-01
  • 2011-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多