tags:

views:

75

answers:

3

Hi there I need to convert a matrix to a list as the example below

Matrix:
[[  1.   6.  13.  10.   2.]
 [  2.   9.  10.  13.  15.]
 [  3.  15.  13.  14.  16.]
 [  4.   5.  14.  13.   6.]
 [  5.  18.  16.   4.   3.]
 [  6.   7.  12.  18.   3.]
 [  7.   1.   8.  17.  11.]
 [  8.  14.   5.   4.  16.]
 [  9.  16.  18.  17.  15.]
 [ 10.   8.   9.  15.  17.]
 [ 11.  11.  17.  18.  12.]]

List:
[(1, 6, 13, 10, 2),  (2, 9, 10, 13, 15), (3, 15, 13, 14, 16),
 (4, 5, 14, 13, 6),  (5, 18, 16, 4, 3),  (6, 7, 12, 18, 3), 
 (7, 1, 8, 17, 11),  (8, 14, 5, 4, 16),  (9, 16, 18, 17, 15),
 (10, 8, 9, 15, 17), (11, 11, 17, 18, 12)]

Thx in adavance

A: 

The best way to do it is:

result = map(tuple, Matrix)
aaronasterling
@gotgenes, correct on both counts. I updated. I generally forget about `chain.from_iterable`. Thanks for the reminder.
aaronasterling
I understand downvoting when I misunderstood the question (which didn't happen), but why has the only answer that actually creates a list of tuples been downvoted _after_ I fixed it? Is it because I look like a hippie? If it makes you feel any better (and not that it's any of your damn business) but I've been shaving at least once every two months and am currently wearing a polo. I'm just too much of a lazy hippie to update my picture.
aaronasterling
@aaronsterling Perhaps the down-voter was the lazy one, reading my two comments and without checking the updated code for correctness? I have removed my earlier two comments since this code is correct (making some assumption about the structure of `Matrix`, which is ill-defined by the OP).
gotgenes
@aaronsterling - Remind me to never post a picture of myself here if looking like a hippie gets you downvoted! :) At any rate I'm not sure why you got downvoted, either... (+1 from me) Your answer is the only one that actually gives a list of tuples. I'm leaving mine up just because `ndarray.tolist()` is useful to know about, even it it produces nested lists.
Joe Kington
+1  A: 

Is this a numpy matrix? If so, just use the tolist() method. E.g.:

import numpy as np
x = np.matrix([[1,2,3],
               [7,1,3],
               [9,4,3]])
y = x.tolist()

This yields:

y --> [[1, 2, 3], [7, 1, 3], [9, 4, 3]]
Joe Kington
A: 

if you are using numpy and you want to just traverse the matrix as a list then you can just

from numpy import array
m = [[  1.   6.  13.  10.   2.]
 [  2.   9.  10.  13.  15.]
 [  3.  15.  13.  14.  16.]
 [  4.   5.  14.  13.   6.]
 [  5.  18.  16.   4.   3.]
 [  6.   7.  12.  18.   3.]
 [  7.   1.   8.  17.  11.]
 [  8.  14.   5.   4.  16.]
 [  9.  16.  18.  17.  15.]
 [ 10.   8.   9.  15.  17.]
 [ 11.  11.  17.  18.  12.]]

for x in array(m).flat:
   print x

This will not consume extra memory

fabrizioM
It also flattens the array, which isn't want the OP wanted (I think?). Regardless, you're quite correct in pointing out that there's no need to convert it to a list if you just want to iterate over it. Simply iterating over the array itself will group the rows, as the OP seems to want.
Joe Kington