0

Working in python 2.7.

I have two lists (simplified to make explanation more clear):

T = [[1, 0], [1, 0], [0, 5], [3, -1]]
B = [[1], [3], [2], [2]]

And three functions.

def exit_score(list):
    exit_scores = []
    length = len(list)
    for i in range(0, length):
        score = list[i][2] - list[i][0]
        exit_scores.append(score)
    return exit_scores

First I append the corresponding values of B to the corresponding lists in T:

def Trans(T, B):
    for t, b in zip(T, B):
        t.extend(b)
    a = exit_score(T)
    b = T
    score_add(b, a)

Then, using the previously listed exit_score function. I subtract the the value in the list[2] position from the value in the list[0] position fore each list. I then append those results to another list (exit_scores).

Finally, I want to add the exit_scores (now a), to the original list.

So I use my score_add(b, a) function which is:

score_add(team, exit_scores):
    for t, e in zip(team, exit_scores)
        t.extend(e)
    print team

If everything were working correctly, I would get an output like this:

[[1,0,1,0], [1,0,3,-2], [0,5,2,-2], [3,-1,2,1]]

Instead, I get a TypeError telling me that I can't iterate over an integer. But I've tried printing both a and b and both are in list form!

When I change the code to make sure that the exit scores are in a list:

def Trans(T, B):
    es = []
    for t, b in zip(T, B):
        t.extend(b)
    es.append(exit_score(T))
    score_add(T, es)

The entire exit_scores list (es) is added to the end of the first list of T:

[[1, 0, 1, 0, 2, 2, -1], [1, 0, 3], [0, 5, 2], [3, -1, 2]]

For the life of me, I can't figure out what I'm doing wrong...

1
  • 2
    You need to re-think all of that code. It's way more complex than it needs to be for a very simple operation. Commented Nov 3, 2011 at 17:17

2 Answers 2

4

This time it's list.append(), not list.extend():

def score_add(team, exit_scores):
    for t, e in zip(team, exit_scores)
        t.append(e)
    print team

B was a list of lists, while exit_scores is a list of integers.

Edit: Here is a cleaned-up version of the whole code:

for t, b in zip(T, B):
    t.extend(b)
    t.append(t[2] - t[0])
Sign up to request clarification or add additional context in comments.

2 Comments

Right you are. Do you know I good resource where I can learn about these commands. Obviously, this area is a bit of a weakness...
@BurtonGuster: I'd start with some Python tutorial. The offficial tutorial is quite good, and I also like How to Think Like a Computer Scientist.
0

I'll bite:

map(lambda x, y: x + y + [x[0] - y[0]], T, B)

Yielding:

[[1, 0, 1, 0], [1, 0, 3, -2], [0, 5, 2, -2], [3, -1, 2, 1]]

Also, it can be done in a list comprehension:

[x+y+[x[0]-y[0]] for x, y in zip(T, B)]

1 Comment

You've reproduced the result the OP didn't want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.