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