Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 什么时候列表不是列表?_Python_List - Fatal编程技术网

Python 什么时候列表不是列表?

Python 什么时候列表不是列表?,python,list,Python,List,下面的代码返回一个列表,例如python中的列表。我访问该列表所做的一切都失败了 索引列表失败, 枚举列表失败 例如,如果我只是打印 但是如果我访问索引 Traceback (most recent call last): File "parse-epoch.py", line 11, in <module> print("losses.add({}, {})".format(s[0], s[1])) IndexError: list index out of range

下面的代码返回一个列表,例如python中的列表。我访问该列表所做的一切都失败了

索引列表失败, 枚举列表失败

例如,如果我只是打印

但是如果我访问索引

Traceback (most recent call last):
  File "parse-epoch.py", line 11, in <module>
    print("losses.add({}, {})".format(s[0], s[1]))
IndexError: list index out of range

您应该再次检查打印输出的内容。您的问题可能是在一行中,s不包含具有2个值的列表。如果这些值不存在,则您无法使用它们。

您需要添加epoch.txt的内容,或者只是在第6行之后添加内容的值。有关更多信息,请参阅。请提供详细信息。似乎您有一个1元素列表,出于某种原因,您认为它包含2个元素。但是,由于您没有为我们提供足够的信息来复制此列表,我们只能猜测问题出在哪里。看起来您的文件中有一个空行这是原始数据文件Epoch[1/40]| d|u loss:0.8216 | g|u loss:1.8973 Epoch[1/40]| d|u loss:0.8570 | g| u loss:1.6021 Epoch[1/40]的一个示例d|U损失:1.0567 g|U损失:2.6506历元[1/40]|d|U损失:0.9862 g|U损失:2.5328历元[1/40]|d|U损失:1.0159 g|U损失:1.3859历元[1/40]| d|U损失:1.0116 g|U损失:1.6647历元[1/40]| d|U损失:0.9844 | g|损失:1。7330@Thediz注释不支持预先格式化的文本。请回答您的问题以添加详细信息。findall值始终返回列表。我查过了。Python是problem@Thediz对于Python,问题在于您是否声称在Python的列表数据结构实现中发现了一个bug?@Thediz它总是返回一个列表是的,但是列表的长度可以变化。
Traceback (most recent call last):
  File "parse-epoch.py", line 11, in <module>
    print("losses.add({}, {})".format(s[0], s[1]))
IndexError: list index out of range
import re

with open('epoch.txt', 'r') as f:
    content = f.readlines()

content = [x.strip() for x in content]

for line in content:
    s = re.findall("\d+\.\d+", line)
    #print(s)
    print("losses.add({}, {})".format(s[0], s[1]))