DGDD
DGDD

Reputation: 1390

Python - Read nth line in a matrix

What is the simplest way I can read the nth letter of a matrix?

I thought this would be possible with a simple for loop but so far I haven't had any luck. The best I can do so far is using a count which is not exactly elegant:

matrix = [[1, 3, 5, 2, 6, 2, 4, 1], [2, 6, 1, 6, 2, 5, 7], [1, 6, 2, 6, 8, 2, 6]]

count = 0
for n in matrix:
     print matrix[count][nth]
     count += 1

For example: Read the 0th number of every row: 1, 2, 1. Read the 4th number of every row: 6, 2, 8.

Upvotes: 1

Views: 4693

Answers (9)

dawg
dawg

Reputation: 103959

Use zip:

>>> matrix = [[1, 3, 5, 2, 6, 2, 4, 1], [2, 6, 1, 6, 2, 5, 7], [1, 6, 2, 6, 8, 2, 6]]
>>> zip(*matrix)[0]
(1, 2, 1)
>>> zip(*matrix)[4]
(6, 2, 8)

Upvotes: 1

martineau
martineau

Reputation: 123481

Here's something that will handle rows of different lengths (as in your example), as well as supporting Python's special interpretation of negative indexes as relative to the end of the sequence (by changing them intolen(s) + n):

NULL = type('NULL', (object,), {'__repr__': lambda self: '<NULL>'})()

def nth_elems(n):
    abs_n = abs(n)
    return [row[n] if abs_n < len(row) else NULL for row in matrix]

matrix = [[1, 3, 5, 2, 6, 2, 4, 1], [2, 6, 1, 6, 2, 5, 7], [1, 6, 2, 6, 8, 2, 6]]

print nth_elems(0)   # [1, 2, 1]
print nth_elems(6)   # [4, 7, 6]
print nth_elems(7)   # [1, <NULL>, <NULL>]
print nth_elems(-1)  # [1, 7, 6]

Upvotes: 2

Steinar Lima
Steinar Lima

Reputation: 7821

Lists in Python are not intended to be used like this. Using list comprehension may cause both memory and CPU issues if the data is sufficiently big. Consider using numpy if this is an issue.

Upvotes: 1

nmgeek
nmgeek

Reputation: 2187

Your for loop is likely not doing what you expect. n is not an integer. It is the current row. I think what you wanted to do was:

for row in matrix:
    print row[0], row[4]

This prints,

1 6
2 2
1 8

Also, strictly speaking, matrix is a list of lists. To really have a matrix you might need to use numpy.

Upvotes: 1

user2555451
user2555451

Reputation:

List comprehensions will work well here:

>>> matrix = [[1, 3, 5, 2, 6, 2, 4, 1], [2, 6, 1, 6, 2, 5, 7], [1, 6, 2, 6, 8, 2, 6]]
>>> # Get all the 0th indexes
>>> a = [item[0] for item in matrix]
>>> a
[1, 2, 1]
>>> # Get all the 4th indexes
>>> b = [item[4] for item in matrix]
>>> b
[6, 2, 8]
>>>

Upvotes: 1

aIKid
aIKid

Reputation: 28292

Here is a solution using list comprehension:

[x[0] for x in matrix]

Which is basically, equal to:

for x in matrix:
    print x[0]

You can also make it a function:

def getColumn(lst, col):
    return [i[col] for i in lst]

Demo:

>>> matrix = [[1, 3, 5, 2, 6, 2, 4, 1], [2, 6, 1, 6, 2, 5, 7], [1, 6, 2, 6, 8, 2, 6]]
>>> def getColumn(lst, col):
    return [i[col] for i in lst]

>>> getColumn(matrix, 0)
[1, 2, 1]
>>> getColumn(matrix, 5)
[2, 5, 2]

Hope this helps!

Upvotes: 1

Akavall
Akavall

Reputation: 86248

If your need to do this operation a lot you could transpose your matrix using zip(*matrix)

>>> matrix = [[1, 3, 5, 2, 6, 2, 4, 1], [2, 6, 1, 6, 2, 5, 7], [1, 6, 2, 6, 8, 2, 6]]
>>> matrix_t = zip(*matrix)
>>> matrix_t
[(1, 2, 1), (3, 6, 6), (5, 1, 2), (2, 6, 6), (6, 2, 8), (2, 5, 2), (4, 7, 6)]
>>> matrix_t[0]
(1, 2, 1)
>>> matrix_t[3]
(2, 6, 6)

Upvotes: 3

viraptor
viraptor

Reputation: 34175

Maybe this way?

column = [row[0] for row in matrix]

(for the 0th element)

Upvotes: 1

inspectorG4dget
inspectorG4dget

Reputation: 114015

In [1]: matrix = [[1, 3, 5, 2, 6, 2, 4, 1], [2, 6, 1, 6, 2, 5, 7], [1, 6, 2, 6, 8, 2, 6]]

In [2]: nth=0

In [3]: [row[nth] for row in matrix]
Out[3]: [1, 2, 1]

In [4]: nth=4

In [5]: [row[nth] for row in matrix]
Out[5]: [6, 2, 8]

Upvotes: 1

Related Questions