Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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_Python 3.x_List - Fatal编程技术网

Python:返回列表的函数的列表声明不同

Python:返回列表的函数的列表声明不同,python,python-3.x,list,Python,Python 3.x,List,因此,我的函数返回一个列表,只要我为固定数量的值定义这样的类,它就可以正常工作。 如果我的searchKeys列表中只有三个元素,并且我定义了returnList,比如returnList=[0,0,0]我可以在调用函数时打印列表。 但是如果我尝试returnList=[]调用函数时,我无法打印列表 returnList = [0,0,0] with open(logfile, "r") as currentFile: # open Eventlog for c, inf

因此,我的函数返回一个列表,只要我为固定数量的值定义这样的类,它就可以正常工作。 如果我的searchKeys列表中只有三个元素,并且我定义了returnList,比如
returnList=[0,0,0]
我可以在调用函数时打印列表。 但是如果我尝试
returnList=[]
调用函数时,我无法打印列表

returnList = [0,0,0]
    with open(logfile, "r") as currentFile: # open Eventlog
        for c, info in enumerate(searchKeys): # loop over searchKeys 
            counter = 0
            currentFile.seek(0)
            for line in currentFile: # loop over lines
                if info in line: # search for element
                    counter += 1
                    returnList[c] = counter # this is where I am trying to fill the
    return returnList

我猜想,由于您试图访问列表中不存在的元素,因此会出现索引超出范围的错误。相反,请使用
append

returnList[c] = counter
应该是

returnList.append(counter)

我认为你遗漏了你计划中最重要的部分。。。
#用n个值填充列表的代码
部分。如果我们知道什么,就会出现问题。。。我们需要对你的问题做一个最低限度的工作演示我同意coldspeed。另外,你遗漏了描述中最重要的部分。出问题的“某物”是什么?希望现在的描述更清楚。实际上,中间的代码从来没有改变,只是第一行中列表的定义。这就是我需要的。此外,我还必须将for循环的“returnList.append(counter)”去掉一个更高级别的范围。