返回列表变量

时间:2017-12-23 13:26:13

标签: python list

我正在创建一个python脚本来从csv文件加载数据。我希望这些数据用于神经网络的训练,所以我希望它的格式是一个tupples(x,y)列表,其中x和y是包含输入和输出的numpy数组。但是当我返回该列表(以下代码中的td)时,我收到此错误' UnboundLocalError:local variable' td'在分配之前引用'

主持人,关于stackoverflow上的这个错误有很多问题,我已经阅读过它们但仍然无法找到解决方案,所以我发布了这个。

import csv
import numpy as np


def load_data():
    // loading the file
    with open('train.csv','rb') as csvfile:
        reader = csv.DictReader(csvfile,delimiter=',')
        for row in reader:
        // these if statements are to check if any of the field in csv file
        // is empty or not
            if(row['start_date'] != ""): 
                a = True
            if(row['sold'] != ""):
                b = True
            if(row['euribor_rate'] != ""):
                c = True
            if(row['libor_rate'] != ""):
                d = True
            if(row['bought'] != ""):
                e = True
            if(row['creation_date'] != ""):
                f = True
            if(row['sell_date'] != ""):
                g = True
            if(row['return'] != ""):
                h = True
            if(a and b and c and d and e and f and g and h):
            // if any of the fields is empty then go to next row 
                pass
            else:
            // now grab the fields 
                mrow = {'sd':row['start_date'],'s':row['sold'],'er':row['euribor_rate'],'lr':row['libor_rate'],'b':row['bought'],'cd':row['creation_date'],'sd':row['sell_date'],'r':row['return']}
                // this will change the data type of fields to float
                int_dict = dict((k,float(v)) for k,v in mrow.iteritems())
                // save an input data field in x
                x = np.array([int_dict['s']])
                // save the output data field in y
                y = np.array([int_dict['r']])
                // put them in tuple
                tuple = (x,y)
                //make a list
                td = []
                // append them to list
                td.append(tuple)
    //return the list
    return td

正如大多数答案所说 - 那就是 - 在函数之外声明td = []然后使用' global td'然后是td.append()。这也没有奏效。然后它没有给出错误,但它返回了一个空列表。

1 个答案:

答案 0 :(得分:1)

您可能没有进入循环的else部分。为避免这种情况,您可能需要将td部分移动到循环的顶部,因此始终定义:

def load_data():
    with open('train.csv','rb') as csvfile:
        reader = csv.DictReader(csvfile,delimiter=',')
        td = []
        for row in reader:
            ...