gpt4 book ai didi

列表值中的python数字格式

转载 作者:行者123 更新时间:2023-11-28 21:38:29 24 4
gpt4 key购买 nike

这是列表

 value = {'a': [['1312.98680732', 14.28625384], 
['1312.98680735', 20.303],
['1312.98680736', 9.78438636]],
'b': [['1307.00000001', 0.02968967],
['1307.00000000', 1.948],
['1306.53301802', 0.02]],
'c': 0,
'd': 226843654}

我能够提取并得到

a = [['1312.98680732', 14.28625384], 
['1312.98680735', 20.303],
['1312.98680736', 9.78438636]]

但是我需要将引用的值从字符串转换为 float 。

期望的输出:

a = [[1312.98680732, 14.28625384], 
[1312.98680735, 20.303],
[1312.98680736, 9.78438636]]

我尝试了 list(map(float, a) 但只有当我有一个值时它才有效。

最佳答案

对于嵌套数据,使用嵌套 list comprehension :

>>> a = [['1312.98680732', 14.28625384], ['1312.98680735', 20.303], ['1312.98680736', 9.78438636]]
>>> [[float(x) for x in group] for group in a]
[[1312.98680732, 14.28625384], [1312.98680735, 20.303], [1312.98680736, 9.78438636]]

关于列表值中的python数字格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48197398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com