python无法访问列表中的值

想法

我有一个简单的代码:

with open('trace.txt') as inf:
    for line in inf:
        parts = line.split()
        lines = float(parts[1])

print lines[0]

但我收到错误,因为无法访问列表中的浮点数:

TypeError: 'float' object has no attribute '__getitem__'

错误的最佳解释是什么以及如何解决这个问题?

亚历克沙

您不能打印浮点数的元素,因为浮点数不是列表,因此没有实现 __getitem__。

lines = []
with open('trace.txt') as inf:
    for line in inf:
        parts = line
        lines.append(float(parts[1]))

print lines[0]

如果要打印列表中的元素,可以使用索引访问它。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章