繁体   English   中英

在 Python 中将元组转换为浮点数

[英]Convert Tuple to Float In Python

我有这个元组

t = (['?'], ['?'], [3], [48.0489692022056], [9.32161088395051])

并且想要将第四个和第五个索引转换为浮点数而不导入任何模块(除了mathgeodist

我需要那个给地质学家

if geodist((x, y), (i[3], i[4]))...:

因为它只接受元组!

错误: TypeError: must be real number, not list

您可以遍历您的元组并为此使用 python 的内置isinstance() function:

t = (['?'], ['?'], ['?'], [48.0489692022056], [9.32161088395051])
tuple([i[0] for i in t[3:] if isinstance(i[0], float)])

Output:

(48.0489692022056, 9.32161088395051)

new_t = (float(t[3]), float(t[4]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM