python浮点到int转换 [英] python float to in int conversion
本文介绍了python浮点到int转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int(20.0)
会导致 20
。到现在为止还挺好。但是:$ $ p $
levels = [int(gex_dict [i])in sort(gex_dict.keys())]
while gex_dict [i]
返回一个浮点数,例如 20.0
,结果是:
base 10:'20 .0'
我只是啃一下键盘的最后一块。
解决方案
'20 .0'
是一个字符串,不是浮
;你可以通过错误信息中的单引号来判断。你可以先用 float
解析 int
,然后用 int
:
>>> int(float('20 .0'))
20
在你的字典中存储花车而不是字符串,因为这是你期望的。)
I have an issue that really drives me mad. Normally doing int(20.0)
would result in 20
. So far so good. But:
levels = [int(gex_dict[i]) for i in sorted(gex_dict.keys())]
while gex_dict[i]
returns a float, e.g. 20.0
, results in:
"invalid literal for int() with base 10: '20.0'"
I am just one step away from munching the last piece of my keyboard.
解决方案
'20.0'
is a string, not a float
; you can tell by the single-quotes in the error message. You can get an int
out of it by first parsing it with float
, then truncating it with int
:
>>> int(float('20.0'))
20
(Though maybe you'd want to store floats instead of strings in your dictionary, since that is what you seem to be expecting.)
这篇关于python浮点到int转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文