在python中嵌套for循环,未定义变量

时间:2015-08-03 19:59:07

标签: python

所以我在Python中有两个列表。两者都是列表清单。当我单独打印行时,它们看起来像这样(除了更大):

架可能

['Name10', 'Ari']
['Name11', 'Atl']
['Name12', 'Bal']
['Name13', 'Bos']
['Name14', 'ChC/CWS']
['Name15', 'Cin']
['Name15', 'Cle']
['Name16', 'Col']
['Name17', 'ChC/CWS']
['Name18', 'Det']

SALARIES

['SP', 'Name1', '6900', 'Tor', '@', 'Sea']
['SP', 'Name2', '6900', 'Hou', '@', 'KC']
['SP', 'Name3', '6900', 'LAD', '@' 'NYM']
['SP', 'Name4', '6800', 'ChC', '@', 'Phi']

问题在于,对于概率列表中的第二个值,有时候它们会有两个东西。见上面的例子,带有斜杠的例子。我想要做的是用正确的值替换该值(基本上选择正确的值)。我决定的方式是与第二个列表进行比较的最佳方法。

最后,例如,如果Probables中的Name14与Salaries中的Name4相同,则Probables中的第5行将显示['Name14','ChC']。这是我到目前为止的代码。

for row in probables:
    if '/' in row[1]:
        a = row[0] #Name from probables list
        b = row[1] #Abbrev. of team
        for row in salaries:
            if row[1] == a:
                c = row[3] #First team involved in game
                d = row[5] #Second team involved in game
    if b == c:
        row[1] = c
    elif b == d:
        row[1] = d

这给了我错误“Traceback(最近一次调用最后一次):   文件“C:\ Users \ Owner \ Desktop \ Test \ matching test.py”,第21行,in     如果b == c: NameError:名称'b'未定义“

我假设这与变量如何工作以及在循环内定义有关,但我不知道如何修复它。有帮助吗?感谢。

3 个答案:

答案 0 :(得分:1)

正如评论已经注意到的那样,这是因为你在'if'/'in row [1]中定义了a,b,c和d:'循环但是大多数条目都不会进入该循环。因此,例如,对于概率中的第1行,只有当'/'在行[1]中时,才会告诉它进入第一个循环。所以它不会进入那个循环。然后你告诉它比较b到c。 b和c尚未定义,因此出错。由于这些行无论如何都不需要改变,因此应该完全跳过它们。您可以再次缩进其他循环,将它们全部置于行[1]'中的'if'\'逻辑下

java.util.concurrent.Executor

答案 1 :(得分:1)

其他解决方案可以回答您的问题(if '/' ...评估为false时未分配的变量)但代码仍存在问题。在第二个循环中,您重复使用相同的变量名称。在此if语句之后,row将始终设置为工资中的最后一行。

我猜这不是你想要的。你应该至少使用一个不同的变量。一旦找到匹配项,您可能还希望突破第二个for循环,这样您就不会不必要地遍历整个列表。您可以{/ 3}}使用

for row in probables:
    if '/' in row[1]:
        a = row[0] #Name from probables list
        b = row[1] #Abbrev. of team
        for salary in salaries:
            if salary[1] == a:
                c = salary[3] #First team involved in game
                d = salary[5] #Second team involved in game
                break         # Stop looping through salaries
        if b == c:
            row[1] = c
        elif b == d:
            row[1] = d

另一个问题是,在您第一次输入if '/' ...语句后,将设置c和d,如果工资列表中没有匹配项,您可以(根据您的数据)假设您当你没有找到一些东西时。为避免这种情况,我将if / else语句移到循环中或添加另一个变量found。或两者兼而有之。

for row in probables:
    if '/' in row[1]:
        a = row[0] #Name from probables list
        b = row[1] #Abbrev. of team
        found = False
        for salary in salaries:
            if salary[1] == a:
                c = salary[3] #First team involved in game
                d = salary[5] #Second team involved in game
                if b == c:
                    row[1] = c
                elif b == d:
                    row[1] = d
                found = True
                break
        if not found:
            print "Can't find team for row"
            print row
            break

答案 2 :(得分:0)

这是因为b和c是"中的局部变量;如果' /'在行[1]:"声明。所以当你进入第二个if语句时,它不知道b和c是什么,除非它是全局定义的。因此,请确保在第二个if语句之前重新定义b和c。

for row in probables:
# define b and c variables here
    if '/' in row[1]:
        a = row[0] #Name from probables list
        b = row[1] #Abbrev. of team
        for row in salaries:
            if row[1] == a:
                c = row[3] #First team involved in game
                d = row[5] #Second team involved in game
    if b == c:
        row[1] = c
    elif b == d:
        row[1] = d