0

So I have two lists in Python. Both are lists of lists. When I print rows individually they look like this (except much bigger):

PROBABLES

['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']

The problem is that for the second value in the Probables list, sometimes they'll have two things in it. See example above, the ones with slashes. What I want to do is replace that value with the correct one (basically selecting the correct one). The way I've decided is the best way to go about this is comparing to a second list.

In the end, for example, the 5th row in Probables would read ['Name14', 'ChC'] if Name14 in Probables was the same as Name4 in Salaries. Here's the code I've got so far.

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

This gives me the error "Traceback (most recent call last): File "C:\Users\Owner\Desktop\Test\matching test.py", line 21, in if b == c: NameError: name 'b' is not defined"

I'm assuming this has to do with how variables work and are defined inside of loops, but I don't know how to fix it. Any help? Thanks.

3
  • 2
    c is only defined if row[1] == a, which also depends on ` '/' in row[1]` Commented Aug 3, 2015 at 19:59
  • There's no pitcher[1] = c in your posted code. Commented Aug 3, 2015 at 20:00
  • @TigerhawkT3 sorry, I had the code I posted here in a comment in my program file and was running it with some different loops that I was experimenting on to try to figure it out, so I posted the wrong error. The actual error here is "Traceback (most recent call last): File "C:\Users\Owner\Desktop\Test\matching test.py", line 21, in <module> if b == c: NameError: name 'b' is not defined" Commented Aug 3, 2015 at 20:03

3 Answers 3

1

The other solutions answer your question (variables not assigned when if '/' ... evaluates to false) but there's still an issue with the code. In the second loop, you're reusing the same variable name. After this if statement, row will always be set to the last row in salaries.

I'm guessing that's not what you want. You should at least use a different variable. You probably also want to break out of the second for loop once you've found a match so that you don't iterate through the entire list unnecessarily. You can use break for that

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

The other issue is that after the first time you enter the if '/' ... statement, c and d will be set and if there is no match in the salaries list, you could (depending on your data) be assuming you found something when you didn't. To avoid that, I'd move the if/else statements to within the loop or add another variable found. Or both.

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
Sign up to request clarification or add additional context in comments.

1 Comment

Oh I did mean to change the variable name in the different loops, I must have forgotten to do that. Thanks.
1

As the comments have already noted, this is because you define a, b, c, and d in the 'if '/' in row[1]:' loop BUT most entries never enter that loop. So for example, for row 1 in probables, you tell it to enter the first loop only if '/' is in row[1]. So it doesn't enter that loop. Then you tell it to compare b to c. b and c haven't been defined, so it errors out. Since those rows don't need to be altered anyways, they should just be skipped entirely. You can just indent your other loops again, putting them all under the logic of 'if '\' in row[1]'

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

2 Comments

I believe this is one of the solutions I tried last night. I just put it in again (and changed the iteration variables for each loop) and now I get the error "Traceback (most recent call last): File "C:\Users\Owner\Desktop\Test\matching test.py", line 21, in <module> if b == c: NameError: name 'c' is not defined." Now it can define c, but not b. EDIT: I think I may not have the correct values in my test files so it may not be findind a vaue to match to c. I'll have to check.
This would only occur if the 'if row[1]==a' loop is never entered, so my guess is that you have some rows in the probables that have no match in the salaries. You might try a little simple print debugging to get at this: maybe have a 'print row' statement right below the 'if '/' in row[1]' statement so you can see which row it errors out on (you could also do this with try/except)
0

This is because b and c are local variables inside of the "if '/' in row[1]:" statement. So when you go into the second if statement, it does not know what b and c is unless it is globally defined. So just make sure you redefine b and c before your second if statement.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.