Python-error

UnboundLocalError: local variable 'y_list' referenced before assignment

代码如下:

def train_pid_name(list_train, pid_name):
    # print('pid_name',pid_name)
    for j in range(1, len(list_train)):
        pid_name_list = list_train[j][0].split(',')
        # print('pid_name_list', pid_name_list)
        # print('pid_name', pid_name_list) #pid_name ['[0', ' 0', ' 1', ' 451', ' 3]']
        train_pid = int(pid_name_list[0][1:])
        train_camid = int(pid_name_list[1])
        train_sid = int(pid_name_list[2])
        train_kid = int(pid_name_list[3])
        train_qid = int(pid_name_list[4][:-1])
        train_pid_name = train_pid + train_camid + train_sid + train_kid + train_qid

        if train_pid_name == pid_name:
            # print('train_pid', train_pid)
            # print('train_camid', train_camid)
            # print('train_sid', train_sid)
            # print('train_kid', train_kid)
            # print('train_qid', train_qid)
            # print('list_train[j]',list_train[j])
            print('y', list_train[j][1])
            print('x', list_train[j][2])
            y_list = list_train[j][1]
            x_list = list_train[j][2]
            # print('y', y)
            # print('x', x)
            # y[21, 27, 27, 45, 43, 28, 42, 48, 67, 94, 120, 65, 86, 110, 17, 18, 16, 20]
            # x[35, 29, 16, 0, 23, 42, 48, 45, 21, 23, 27, 40, 63, 46, 32, 38, 25, 42]
    return y_list, x_list

修改如下: 

def train_pid_name(list_train, pid_name):
    # print('pid_name',pid_name)
###修改之处:添加global( 定义返回值y_list和x_list为全局变量)###
    global y_list
    global x_list
############################################################
    for j in range(1, len(list_train)):
        pid_name_list = list_train[j][0].split(',')
        # print('pid_name_list', pid_name_list)
        # print('pid_name', pid_name_list) #pid_name ['[0', ' 0', ' 1', ' 451', ' 3]']
        train_pid = int(pid_name_list[0][1:])
        train_camid = int(pid_name_list[1])
        train_sid = int(pid_name_list[2])
        train_kid = int(pid_name_list[3])
        train_qid = int(pid_name_list[4][:-1])
        train_pid_name = train_pid + train_camid + train_sid + train_kid + train_qid

        if train_pid_name == pid_name:
            # print('train_pid', train_pid)
            # print('train_camid', train_camid)
            # print('train_sid', train_sid)
            # print('train_kid', train_kid)
            # print('train_qid', train_qid)
            # print('list_train[j]',list_train[j])
            print('y', list_train[j][1])
            print('x', list_train[j][2])
            y_list = list_train[j][1]
            x_list = list_train[j][2]
            # print('y', y)
            # print('x', x)
            # y[21, 27, 27, 45, 43, 28, 42, 48, 67, 94, 120, 65, 86, 110, 17, 18, 16, 20]
            # x[35, 29, 16, 0, 23, 42, 48, 45, 21, 23, 27, 40, 63, 46, 32, 38, 25, 42]
    return y_list, x_list

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值