user127815
user127815

Reputation: 141

Python List not appending items to list

Can't figure out why, help appreciated. It is just returning [] each time. Edited with arguments, sorry.

def in(f, e):
    n = len(f)
    a = 0
    b = 0 
    c = 0
    m = []
    for i in range (1, n):
        a = 0
        for j in range (0, n + 1):
            if (e[i - 1] == (j, i)):
                a = a + 1
                m.append(a)
    return m

print(in([1, 2, 3] , [(1, 2), (2, 1), (3, 2)]))

Upvotes: 0

Views: 730

Answers (1)

Tim
Tim

Reputation: 43314

for j in range (0, n + 1):
    print e[i - 1],
    print (j, i)
    if (e[i - 1] == (j, i)):

I added these two prints for testing purposes and below is their output. (I'm using python 2.7, hence the statements instead of functions, but that doesn't really make a difference)

(1, 2) (0, 1)
(1, 2) (1, 1)
(1, 2) (2, 1)
(1, 2) (3, 1)
(2, 1) (0, 2)
(2, 1) (1, 2)
(2, 1) (2, 2)
(2, 1) (3, 2)

As you can see, e[i - 1] is never equal to (j, i), therefore it won't ever enter the if block and won't append items to your list, so it stays empty and your function returns [].

Btw, I also had to change the function name because as mentioned in the comments, def in(f, e): produces a SyntaxError.

Upvotes: 3

Related Questions