python-3.x - 列表列表中的Python 3列表理解以转换类型

标签 python-3.x list loops list-comprehension

考虑以下列表列表:

list1 = [['1.1', '1.2', '1.3'], ['2.1', '2.2', '2.3'], ...]

要理解一个字符串列表以将它们转换为可以使用的 float

list1[0] = [float(i) for i in list1[0]]

但我试图理解 float 列表的列表并没有奏效:

list1 = [[float(j) for j in list1[i]] for i in list1]

由于

TypeError: list indices must be integers or slices, not list

有没有一种方法可以在不显式使用循环的情况下进行这种列表理解?

最佳答案

[[float(j) for j in i] for i in list1]

应该做

关于python-3.x - 列表列表中的Python 3列表理解以转换类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56413983/

相关文章:

python - 子进程运行/调用不起作用

Python - 发生意外导入

Python,如何替换列表中元素的多个部分(在列表中)

python - 当子字符串匹配时如何映射两个列表中的值

java - 将 iText 列表项目符号更改为黑点

c - 是否可以真正强制变量变为常量以允许我动态声明数组?

loops - Vuejs v-for 使用 Array.prototype.reverse() 的无限更新循环

swift - 如何使用 swift 解析字典中的 Any

python - 如何以格式化字符串打开文件?

python - Pygame Sprite 运动故障