【问题标题】:Python list will print but not returnPython列表将打印但不返回
【发布时间】:2017-07-03 03:26:27
【问题描述】:

我正在编写获取 csv 文件的代码,然后返回 csv 文件中每一行的数据点列表。列表将按日期和位置排序。

代码按照我想要的方式创建列表,但在调用时不返回列表。

第一个函数读取文件并生成数据点。第二个函数调用第一个函数,排序并(希望)返回数据

def CreateDataStructure(data):
allData=[]
with open(data,'r') as data:

    dataRead=data.readlines()
    for line in dataRead[1:]:
        splitList=line.split(",")
        dataPoint =[splitList[25],splitList[1],{splitList[19]:splitList[9]}]
        allData.append(dataPoint)
sortedData=sorted(allData)
return sortedData


def compileData(filename,counter,sortedData):    
    if counter==0:
        sortedData=CreateDataStructure(filename)
        compileData(filename,1,sortedData)
    else:
        if counter<len(sortedData):

            if sortedData[0][0]==sortedData[1][0] and sortedData[0][1]==sortedData[1][1]:#change these back
                compDict=dict(list(sortedData[0][2].items())+list(sortedData[1][2].items()))
                sortedData[0]=[sortedData[0][0],sortedData[0][1],compDict]    
                sortedData.pop(1)
                compileData(filename,counter,sortedData)
                counter=counter+1
            else:
                sortedData+=[sortedData.pop(0)]
                counter=counter+1
                compileData(filename,counter,sortedData)
        else:
           from itertools import groupby
            for key, locationGroup in groupby(sortedData, lambda x: x[0]):
                bigList=[]
                smallList=[]
                    for date in locationGroup:
                    smallList.append(date)
                    bigList.append(smallList)
            print bigList
            return bigList

print compileData("fakeData.csv",0,[]) 

当我运行此代码时,它会打印出我想要的内容(biglist,如果您想知道,我将其粘贴在下面)但返回 None(noneType 对象)。 为什么退货和打印会出现两种不同的情况,我该如何解决这个问题

[[['744701', '40974', {'Alkalinity': '234'}], ['744701', '41018', {'Alkalinity': '252'}], ['744701', '41058', {'Alkalinity': '270.53'}]], [['744701', '40974', {'Alkalinity': '234'}], ['744701', '41018', {'Alkalinity': '252'}], ['744701', '41058', {'Alkalinity': '270.53'}]], [['744701', '40974', {'Alkalinity': '234'}], ['744701', '41018', {'Alkalinity': '252'}], ['744701', '41058', {'Alkalinity': '270.53'}]]]

【问题讨论】:

  • 您在该代码中仅使用了一个返回语句 (compileData())。在某些情况下,您的 if 语句没有返回值。
  • 我猜你也想交换bigList=[]for key, locationGroup...这行。此外,您可能需要在每次调用compileData 之前返回。最后但同样重要的是:请在发布前检查代码格式。
  • 我会推荐使用pandas 来做这种事情,pandas.read_csv 会在 csv 文件中读取,你可以很容易地做到groupbys - 我不完全确定从 csv 到所需输出列表的映射是什么

标签: python list return nonetype


【解决方案1】:

您的compileData 函数有多个执行分支,这意味着它可以在多个点返回。但是,只有 一个counter !=0 and counter &gt;= len(sortedData) 即最后一个 else 分支会返回任何内容。

例如:

if counter==0:
    sortedData=CreateDataStructure(filename)
    compileData(filename,1,sortedData)

即使compileData 调用设法到达最后一个else 分支,即打印并返回结果的分支,实际上并没有对结果进行任何处理。这意味着在compileData(filename,1,sortedData)完成后,它返回的数据将被丢弃,函数继续执行直到它最终到达末尾并隐式返回None

这应该可以解决那个问题:

def compileData(filename,counter,sortedData):    
    if counter==0:
        sortedData=CreateDataStructure(filename)
        return compileData(filename,1,sortedData)
    else:
        if counter<len(sortedData):

            if sortedData[0][0]==sortedData[1][0] and sortedData[0][1]==sortedData[1][1]:#change these back
                compDict=dict(list(sortedData[0][2].items())+list(sortedData[1][2].items()))
                sortedData[0]=[sortedData[0][0],sortedData[0][1],compDict]    
                sortedData.pop(1)
                counter=counter+1
                return compileData(filename,counter,sortedData)
            else:
                sortedData+=[sortedData.pop(0)]
                counter=counter+1
                return compileData(filename,counter,sortedData)
        else:
           from itertools import groupby
            for key, locationGroup in groupby(sortedData, lambda x: x[0]):
                bigList=[]
                smallList=[]
                    for date in locationGroup:
                    smallList.append(date)
                    bigList.append(smallList)
            print bigList
            return bigList

更新

您可以通过returning early 使您的函数(主观上)更容易理解,而不是嵌套多个ifs。您的函数将如下所示:

from itertools import groupby
...
def compileData(filename,counter,sortedData):
    if counter==0:
        sortedData=CreateDataStructure(filename)
        return compileData(filename,1,sortedData)

    if counter<len(sortedData):
        if sortedData[0][0]==sortedData[1][0] and sortedData[0][1]==sortedData[1][1]:#change these back
            compDict=dict(list(sortedData[0][2].items())+list(sortedData[1][2].items()))
            sortedData[0]=[sortedData[0][0],sortedData[0][1],compDict]    
            sortedData.pop(1)
            counter=counter+1
            return compileData(filename,counter,sortedData)

        sortedData+=[sortedData.pop(0)]
        counter=counter+1
        return compileData(filename,counter,sortedData)

    for key, locationGroup in groupby(sortedData, lambda x: x[0]):
        bigList=[]
        smallList=[]
            for date in locationGroup:
            smallList.append(date)
            bigList.append(smallList)

    print bigList
    return bigList

【讨论】:

  • 您还有其他问题吗?
猜你喜欢
  • 2017-05-05
  • 1970-01-01
  • 2018-08-06
  • 2014-11-16
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多