考虑以下列表列表:
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/