Question

For some obscure reason I can access the elements of a tuple, but I can't do it if it is a list.

def __init__(self, collider, x, y, gridKeys, tilesetGids, collisionGids):

    self.gridKeys = gridKeys
    self.gids = tilesetGids
    for i in self.gridKeys:
        if ((i[0][0] == x) and (i[0][1] == y)):
            print i

The argument gridKeys is a list of tuples with two elements (The first being another tuple, and the second being a string). Here's the gridKeys:

[((0, 0), '1'), ((0, 1), '1'), ((0, 2), '1'), ((0, 3), '1'), ((0, 4), '1'), ((0, 5), '1'), ((0, 6), '1'), ((0, 7), '1'), ((0, 8), '1'), ((0, 9), '1'), ((1, 0), '1'), ((1, 1), '0'), ((1, 2), '0'), ((1, 3), '0'), ((1, 4), '1'), ((1, 5), '1'), ((1, 6), '0'), ((1, 7), '0'), ((1, 8), '0'), ((1, 9), '1'), ((2, 0), '1'), ((2, 1), '0'), ((2, 2), '0'), ((2, 3), '0'), ((2, 4), '0'), ((2, 5), '0'), ((2, 6), '0'), ((2, 7), '0'), ((2, 8), '0'), ((2, 9), '1'), ((3, 0), '1'), ((3, 1), '0'), ((3, 2), '0'), ((3, 3), '0'), ((3, 4), '0'), ((3, 5), '0'), ((3, 6), '0'), ((3, 7), '0'), ((3, 8), '0'), ((3, 9), '1'), ((4, 0), '1'), ((4, 1), '0'), ((4, 2), '0'), ((4, 3), '0'), ((4, 4), '0'), ((4, 5), '0'), ((4, 6), '0'), ((4, 7), '0'), ((4, 8), '0'), ((4, 9), '1'), ((5, 0), '1'), ((5, 1), '3'), ((5, 2), '0'), ((5, 3), '0'), ((5, 4), '0'), ((5, 5), '0'), ((5, 6), '0'), ((5, 7), '0'), ((5, 8), '0'), ((5, 9), '1'), ((6, 0), '1'), ((6, 1), '1'), ((6, 2), '1'), ((6, 3), '1'), ((6, 4), '0'), ((6, 5), '0'), ((6, 6), '0'), ((6, 7), '0'), ((6, 8), '4'), ((6, 9), '1'), ((7, 0), '1'), ((7, 1), '0'), ((7, 2), '0'), ((7, 3), '0'), ((7, 4), '0'), ((7, 5), '0'), ((7, 6), '0'), ((7, 7), '1'), ((7, 8), '1'), ((7, 9), '1'), ((8, 0), '1'), ((8, 1), '0'), ((8, 2), '0'), ((8, 3), '0'), ((8, 4), '0'), ((8, 5), '0'), ((8, 6), '0'), ((8, 7), '1'), ((8, 8), '1'), ((8, 9), '1'), ((9, 0), '1'), ((9, 1), '1'), ((9, 2), '1'), ((9, 3), '1'), ((9, 4), '1'), ((9, 5), '1'), ((9, 6), '1'), ((9, 7), '1'), ((9, 8), '1'), ((9, 9), '1')]

As you can see in my iteration up there, I test if the first and second elements of each inner tuple inside each outer tuple is equal to the x and y arguments. If it's true, it prints the whole outer tuple:

((2, 3), '0') # I've passed a 2 for the x argument and a 3 for the y argument.

Now, for some obscure reason, if I convert each outer tuple to a list, I cannot access the elements:

def __init__(self, collider, x, y, gridKeys, tilesetGids, collisionGids):

    self.gridKeys = [list(x) for x in gridKeys] # Converting each tuple to a mutable sequence.
    self.gids = tilesetGids
    for i in self.gridKeys:
        if ((i[0][0] == x) and (i[0][1] == y)):
            print i

There's no output for this. The result is not true for some reason, but, if I print the converted gridKeys:

[[(0, 0), '1'], [(0, 1), '1'], [(0, 2), '1'], [(0, 3), '1'], [(0, 4), '1'], [(0, 5), '1'], [(0, 6), '1'], [(0, 7), '1'], [(0, 8), '1'], [(0, 9), '1'], [(1, 0), '1'], [(1, 1), '0'], [(1, 2), '0'], [(1, 3), '0'], [(1, 4), '1'], [(1, 5), '1'], [(1, 6), '0'], [(1, 7), '0'], [(1, 8), '0'], [(1, 9), '1'], [(2, 0), '1'], [(2, 1), '0'], [(2, 2), '0'], [(2, 3), '0'], [(2, 4), '0'], [(2, 5), '0'], [(2, 6), '0'], [(2, 7), '0'], [(2, 8), '0'], [(2, 9), '1'], [(3, 0), '1'], [(3, 1), '0'], [(3, 2), '0'], [(3, 3), '0'], [(3, 4), '0'], [(3, 5), '0'], [(3, 6), '0'], [(3, 7), '0'], [(3, 8), '0'], [(3, 9), '1'], [(4, 0), '1'], [(4, 1), '0'], [(4, 2), '0'], [(4, 3), '0'], [(4, 4), '0'], [(4, 5), '0'], [(4, 6), '0'], [(4, 7), '0'], [(4, 8), '0'], [(4, 9), '1'], [(5, 0), '1'], [(5, 1), '3'], [(5, 2), '0'], [(5, 3), '0'], [(5, 4), '0'], [(5, 5), '0'], [(5, 6), '0'], [(5, 7), '0'], [(5, 8), '0'], [(5, 9), '1'], [(6, 0), '1'], [(6, 1), '1'], [(6, 2), '1'], [(6, 3), '1'], [(6, 4), '0'], [(6, 5), '0'], [(6, 6), '0'], [(6, 7), '0'], [(6, 8), '4'], [(6, 9), '1'], [(7, 0), '1'], [(7, 1), '0'], [(7, 2), '0'], [(7, 3), '0'], [(7, 4), '0'], [(7, 5), '0'], [(7, 6), '0'], [(7, 7), '1'], [(7, 8), '1'], [(7, 9), '1'], [(8, 0), '1'], [(8, 1), '0'], [(8, 2), '0'], [(8, 3), '0'], [(8, 4), '0'], [(8, 5), '0'], [(8, 6), '0'], [(8, 7), '1'], [(8, 8), '1'], [(8, 9), '1'], [(9, 0), '1'], [(9, 1), '1'], [(9, 2), '1'], [(9, 3), '1'], [(9, 4), '1'], [(9, 5), '1'], [(9, 6), '1'], [(9, 7), '1'], [(9, 8), '1'], [(9, 9), '1']]

The only difference is that now each outer tuple is a list, and, theoretically, I should be able to access (__ getitem __) them in the exact same way. Why I can't access the list element if it is not a tuple?

Was it helpful?

Solution

In Python 2.7, your input x variable is redefined to be the last element of gridKeys.

In Python 3.3, this doesn't happen.

Simple solution is to use different variable names.

OTHER TIPS

Found the problem:

def __init__(self, collider, x, y, gridKeys, tilesetGids, collisionGids):

    self.gridKeys = [list(PROBLEMHERE) for PROBLEMHERE in gridKeys] # Converting each tuple to a mutable sequence.
    self.gids = tilesetGids
    for i in self.gridKeys:
        if ((i[0][0] == x) and (i[0][1] == y)):
            print i
    self.collisionGids = collisionGids

I was using "x" for the argument inside the list-comprehension, and that was confusing the x argument (My distraction/fault... But that's an obscure way to "get distracted".) The x argument is just an int number, but the x inside the list comprehension was a whole tuple with two elements (And that tuple was the one that was being tested, and that's why the result was false).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top