-1

Suppose you have tuple of tuples(looks like a matrix). Now I want to change its contents, so I convert it into a list. Suppose that I have the number of rows and cols of the matrix.

How can I match between the indexes in the matrix to the indexes in the list?

Thanx in advance.

adamco
  • 147
  • 6
  • Are you saying that you flattened the matrix i.e. you made it a one-dimensional list? Otherwise, the indexes are the same. (Also, please add a minor example of tuple of tuples.) – syockit Nov 16 '13 at 08:36
  • exactly. I converted it into one-dimentional. – adamco Nov 16 '13 at 08:39

3 Answers3

4

With a list, you can simply use the [] operator again. So, for example:

>>> a = [[1,2], [3, 4]]
>>> a[0][0]
1
>>> a[0][1]
2
>>> a[1][0]
3
>>> a[1][1]
4
>>> type(a)
<type 'list'>
>>> type(a[0])
<type 'list'>
>>> type(a[0][0])
<type 'int'>

The explanation is simple, the first time, you use the [] operator, you get a list, so you can use the [] operator again. Like this, you can emulate a matrix.

If you want to find the indexes, then you can use this nifty little function:

def finder(value_to_find, matrix):
    for r, row in enumerate(matrix):
        for c, col in enumerate(row):
            if col == value_to_find:
                return r, c

And, for a demo:

>>> a = [[1,2], [3, 4]]
>>> a[0][0]
1
>>> a[0][1]
2
>>> a[1][0]
3
>>> a[1][1]
4
>>> def finder(value_to_find, matrix):
    for r, row in enumerate(matrix):
        for c, col in enumerate(row):
            if col == value_to_find:
                return r, c
>>> finder(4, a)
(1, 1)

And here is an explanation with comments:

def finder(value_to_find, matrix):
    """
    Function to find the indexes of a given value, on first notice
    @param value_to_find: The value we need to find
    @param matrix: The matrix we are to work with
    @return: A tuple of row, column
    """
    # Looping over the rows (lists within the main matrix)
    for r, row in enumerate(matrix):   # Using enumerate returns the index, r and the value row (which is a list)
        for c, col in enumerate(row):  # Looping over the values in each row, with index c and value col
            if col == value_to_find:  # If the col is equal to the value we want, then we return the row, column tuple
                return r, c

If you have a 1-D matrix, then you can look at this solution from Hyperborius:

listindex = row * length_of_row + column
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
3
a = [[1,2], [3, 4]]

if a is the matrix, we can access the individual elements by using two parameters, row and column. Here, row refers to the number of list and column will refer to the position of the element in the list.

So, column is nothing but the normal way of referencing an element in the list and row is nothing but the normal way of referencing a list inside a list. Both row and column are zero based indices.

The format is

a[row][column]

when we say

a[row]

it means that, from the list of lists, get the list at position row and when we say

a[row][column]

we say that, from the list which we want, pick the element at position column.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

Assuming you have a tuple of tuples:

>>> a = ((1,2,3),(4,5,6),(7,8,9))

which you converted to a flat list, presumably using a technique like this (credits to Joel Cornett, https://stackoverflow.com/a/10636583/219229):

>>> b = list(sum(a, ()))
>>> b
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Such that b effectively lost the original multi-dimensional indexing. If you already know the original index from a, you can calculate its index in b as following:

>>> matrix_width = len(a[0])
... (assuming you have the indices i,j) ...
>>> index_in_b = j*matrix_width + i 
>>> item_to_find = b[index_in_b]

If you expand this to multi-dimensional arrays, like 3d-array, and you have indices i, j, k, then it should be index = i + (j * width) + (k * width * span), where width = a[0][0], and span = a[0]


P/S: Just in case you want to convert it to list of lists, here are some shorthands you can use:

>>> b = list[map(list,a)]  # using map
>>> b = [list(x) for x in a]  # using list comprehension
Community
  • 1
  • 1
syockit
  • 5,747
  • 1
  • 24
  • 33