Skip to main content
 首页 » 编程设计

python之更改嵌套列表中的 float

2025年01月19日13lhb25

我是 python 的新手,我现在面临一个小挑战 我有这个列表 nested_list = [['1','2','3'],['2','4','6']] 我想将列表的字符串更改为它们的浮点表示 同时保持顺序不变

new_list = [[1.0,2.0,3.0],[2.0,4.0,6.0]] 

感谢帮助

请您参考如下方法:

为每个嵌套列表中的每个项目调用float:

new_list = [[float(x) for x in lst] for lst in nested_list]