Python 接收:“;UnboundLocalError:赋值前引用的局部变量;
我见过多个线程的人都有相同的问题,但由于问题的独特性,似乎解决方案是根据具体情况提供的 这是我的密码:Python 接收:“;UnboundLocalError:赋值前引用的局部变量;,python,python-2.7,variable-assignment,Python,Python 2.7,Variable Assignment,我见过多个线程的人都有相同的问题,但由于问题的独特性,似乎解决方案是根据具体情况提供的 这是我的密码: loga = [(912, "Message A1") , (1000, "Message A2") , (988, "Message A3") , (1012, "Message A4") , (1002, "Message A5")] logb = [(926, "Message B1") , (1008, "Message B2") , (996, "Message B3") , (10
loga = [(912, "Message A1") , (1000, "Message A2") , (988, "Message A3") , (1012, "Message A4") , (1002, "Message A5")]
logb = [(926, "Message B1") , (1008, "Message B2") , (996, "Message B3") , (1019, "Message B4") , (1100, "Message B5")]
logc = [(1056,"Message C1") , (1033, "Message C2") , (999, "Message C3") , (1054, "Message C4") , (1086, "Message C5")]
logs = [loga, logb, logc]
out = []
def find_lowest_i(lst):
for i in range(len(lst)):
log = lst[i]
if log:
t = log[0][0]
if i==0 or t < lowest_t:
lowest_i = i
lowest_t = t
return lowest_i
while True:
i = find_lowest_i(logs)
print "i=", i
tpl = logs[i].pop(0)
print tpl
out.append(tpl)
print out
loga=[(912,“消息A1”),(1000,“消息A2”),(988,“消息A3”),(1012,“消息A4”),(1002,“消息A5”)]
logb=[(926,“消息B1”),(1008,“消息B2”),(996,“消息B3”),(1019,“消息B4”),(1100,“消息B5”)]
logc=[(1056,“消息C1”),(1033,“消息C2”),(999,“消息C3”),(1054,“消息C4”),(1086,“消息C5”)]
logs=[loga,logb,logc]
out=[]
def查找最低值(lst):
对于范围内的i(len(lst)):
log=lst[i]
如果记录:
t=log[0][0]
如果i==0或t<最低值:
最低_i=i
最低t=t
返回最低值
尽管如此:
i=查找最低值(日志)
打印“i=”,i
tpl=logs[i].pop(0)
打印第三方物流
out.append(tpl)
打印出来
我的确切错误是:
“消息文件名行位置回溯19查找最低值13UnboundLocalError:在赋值“之前引用的局部变量“lower\t”由于连续的
pop
ing,其中一个日志(包含此数据的第一个日志)最终将为空:
logs = [[],
[(1019, 'Message B4'), (1100, 'Message B5')],
[(1056, 'Message C1'), (1033, 'Message C2'), (999, 'Message C3'),
(1054, 'Message C4'), (1086, 'Message C5')]]
在这种情况下,if log
将在i=0时失败,并且lower\u t
未初始化。
在下一次迭代中,i=1,有一个
log
,但没有lower\t
。例外 由于连续的pop
ing,其中一个日志(包含此数据的第一个日志)最终将为空:
logs = [[],
[(1019, 'Message B4'), (1100, 'Message B5')],
[(1056, 'Message C1'), (1033, 'Message C2'), (999, 'Message C3'),
(1054, 'Message C4'), (1086, 'Message C5')]]
在这种情况下,if log
将在i=0时失败,并且lower\u t
未初始化。
在下一次迭代中,i=1,有一个
log
,但没有lower\t
。例外 不确定你有什么麻烦…不确定你有什么麻烦…啊,我明白了!这是有道理的。很明显,我对python不是很精通。我会使用例外吗?啊,我明白了!这是有道理的。很明显,我对python不是很精通。我会使用例外吗?