【发布时间】:2021-02-21 10:28:32
【问题描述】:
lst = ['a: 12', 'b: 1.5', 'c: 13']
for lines in lst:
entry = lines.split(":")
category = entry[0]
amount = entry[1].strip()
print(amount.isnumeric())
结果为 True,False,isnumeric、isdigit 和 isdecimal 为 True
最终使用 isinstance(float(amount), float) 来测试这种情况
【问题讨论】:
-
因为
.不是数字。 -
因为
.不是数字,不是数字,也不是十进制。 -
那么对于像 1.5 这样的数字返回 true 的最佳方法是什么?
-
这取决于您认为“像 1.5 这样的数字”。你会考虑
.1或1.这样的数字吗?1E9呢?inf和nan数字一样吗?