python - 如何在 Python 中索引浮点值?

标签 python indexing

只是寻找问题的答案,再多的谷歌搜索似乎也无法解决。

如果..

a = 1.23

我希望能够取 1 并乘以这个数字,但保留 0.23

这怎么可能?

提前致谢!

最佳答案

在对 munkhd 的回答的评论中,您说:

I have want to be able to input a value as hours then convert them to minutes. So if it was 1.20 I would multiply the 1 by 60 then add 20. Im sure there must be an easier method :)

因此,您的程序将收到 1.20 作为字符串。所以你可以在上面使用字符串方法,例如

>>> dotted_time = '1.20'
>>> h, m = [int(s) for s in dotted_time.split('.')]
>>> print h, m
1 20
>>> minutes = 60*h + m
>>> hours = minutes / 60.0
>>> print minutes, hours
80 1.33333333333

或者,您可以执行 colon_time = dotted_time.replace('.', ':'); colon_time 采用标准时间函数可以理解和操作的格式。这可能是更明智的继续方式,如果您想处理像 '1.20.30' 这样的秒时间,.replace('.', ':') 将转换它,那么它会很容易处理到“1:20:30”。

关于python - 如何在 Python 中索引浮点值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25919129/

相关文章:

python - 将字符串转换为元组对的序列

oracle - 在 Oracle 中,唯一约束是否隐式包含索引?

python - 读取/写入 Parquet 文件而不读入内存(使用 Python)

python - 在Python中向图像添加对齐文本

python - 如何使用 Pandas 从一个数据框中的另一个数据框中查找值?

SQLITE 和自动索引

delphi - 如果使用索引,.Locate 会更快吗?

mysql - 列上的索引 - 两个单独的索引 VS 一组索引?

python django 关闭 excel 服务器

Python H2O直方图错误: bar() missing 1 required positional argument: 'x'