V Anon
V Anon

Reputation: 543

finding the index of elements in nested list

I am trying to create a program that will find and store the index of each element in the nested list.

So far, I have tried using nested for iterators to accomplish this.

Below is my code.

table = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
def coordinates(table):
    target_cell_list = []
    for row in table:
        for column in row:
            target_cell = (table.index(row), row.index(column))
            target_cell_list.append(target_cell)
    return target_cell_list

>>> table = [[1, 1, 1], [2, 2, 3], [2, 3, 3]]
>>> coordinates(table)
# current output
[(0, 0), (0, 0), (0, 0), (1, 0), (1, 0), (1, 2), (2, 0), (2, 1), (2, 1)]

# desired output
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

I think the row indexes are outputting in a correct manner but the column indexes are doing something strange.

I have looked over the code several times but I cannot find out what's wrong with it.

Upvotes: 4

Views: 1776

Answers (3)

user2390182
user2390182

Reputation: 73498

A nested comprehension using enumerate will do:

table = [[1, 1, 1], [2, 2, 3], [2, 3, 3]]

def coordinates(tbl):
    return [(i, j) for i, row in enumerate(tbl) for j, _ in enumerate(row)]
    # or a little shorter
    # [(i, j) for i, row in enumerate(tbl) for j in range(len(row))]

coordinates(table)
# [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

Your list.index(elmnt) based approach fails because index always returns the first index of the element in the list, so it won't work if there are repetitions. Also, it has worse performance since every index call must iterate the list it is called on. A pure loop-index-based implementation along your original lines would be:

def coordinates(tbl):
    target_cell_list = []
    for i in range(len(tbl)):
        for j in range(len(tbl[i])):
            target_cell_list.append((i, j))
    return target_cell_list

And if you know that your table is not jagged, you can use itertools.product:

from itertools import product

def coordinates(tbl):
    return list(product(range(len(tbl)), range(len(tbl[0]))))

Upvotes: 5

Somya Avasthi
Somya Avasthi

Reputation: 121

If you observe carefully, then you may observe that there are duplicate values in the list that is the reason you get the indexes wrong. So, in case of duplications you can use enumerate. Enumerate will return the tuple object so iiteration over it and this will give you expected output.

You can try this-

def coordinates(table):
    target_cell_list = []
    for i,row in enumerate(table):
        for j in range(len(row)):
            target_cell = (i,j)
            target_cell_list.append(target_cell)
    return target_cell_list
table = [[1, 1, 1], [2, 2, 3], [2, 3, 3]]
print(coordinates(table))

Upvotes: 0

timgeb
timgeb

Reputation: 78780

Here's a numpy solution.

>>> import numpy as np
>>> list(zip(*np.where(np.ones_like(table))))                                                                     
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

Upvotes: 1

Related Questions