Eliminate units when using quantities for Python

跟風遠走 提交于 2019-12-14 01:22:58

问题


I'm using the great quantities package for Python. I would like to know how I can get at just the numerical value of the quantity, without the unit.

I.e., if I have

E = 5.3*quantities.joule

I would like to get at just the 5.3. I know I can simply divide by the "undesired" unit, but hoping there was a better way to do this.


回答1:


E.item() seems to be what you want, if you want a Python float. E.magnitude, offered by tzaman, is a 0-dimensional NumPy array with the value, if you'd prefer that.

The documentation for quantities doesn't seem to have a very good API reference.




回答2:


I believe E.magnitude gets you what you want.




回答3:


>>> import quantities
>>> E=5.3*quantities.joule
>>> E.item()
5.3


来源:https://stackoverflow.com/questions/9403415/eliminate-units-when-using-quantities-for-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!