1

i have a list like this:

A=[["a_00",0,0],["a_01",0,1],["a_02",0,2],["a_03",0,3], ["a_10",1,0],["a_11",1,1],["a_12",1,2],["a_13",1,3], ["a_20",2,0],["a_21",2,1],["a_22",2,2],["a_23",2,3], ["a_30",3,0],["a_31",3,1],["a_32",3,2],["a_33",3,3]]

which yields:

In [187]: A
Out[187]:
[['a_00', 0, 0],
 ['a_01', 0, 1],
 ['a_02', 0, 2],
 ['a_03', 0, 3],
 ['a_10', 1, 0],
 ['a_11', 1, 1],
 ['a_12', 1, 2],
 ['a_13', 1, 3],
 ['a_20', 2, 0],
 ['a_21', 2, 1],
 ['a_22', 2, 2],
 ['a_23', 2, 3],
 ['a_30', 3, 0],
 ['a_31', 3, 1],
 ['a_32', 3, 2],
 ['a_33', 3, 3]]

i want to turn in to a matrix like this:

B=[["a_00","a_01","a_02","a_03"], ["a_10","a_11","a_12","a_13"], ["a_20","a_21","a_22","a_23"], ["a_30","a_31","a_32","a_33"]] 

yields:

In [188]: B
Out[188]:
[['a_00', 'a_01', 'a_02', 'a_03'],
 ['a_10', 'a_11', 'a_12', 'a_13'],
 ['a_20', 'a_21', 'a_22', 'a_23'],
 ['a_30', 'a_31', 'a_32', 'a_33']]

i wrote this code for my purpose:

import numpy
B=numpy.zeros(7,7)
for item in A:
    B[item[1]][item[2]]=item[0]

but i see this error:

IndexError: list index out of range

what should i do?

5
  • can you provide a small, but reproducible input data set and your desired data set? Commented Aug 19, 2017 at 18:52
  • Possible duplicate of how to construct a matrix from lists in Python? Commented Aug 19, 2017 at 18:55
  • 1
    What are the types of a_00, a_01 etc? String or numeric? Commented Aug 19, 2017 at 19:02
  • @MaxU: for example A=[["a_00",0,0],["a_01",0,1],["a_02",0,2],["a_03",0,3], ["a_10",1,0],["a_11",1,1],["a_12",1,2],["a_13",1,3], ["a_20",2,0],["a_21",2,1],["a_22",2,2],["a_23",2,3], ["a_30",3,0],["a_31",3,1],["a_32",3,2],["a_33",3,3]] B=[["a_00","a_01","a_02","a_03"], ["a_10","a_11","a_12","a_13"], ["a_20","a_21","a_22","a_23"], ["a_30","a_31","a_32","a_33"]] Commented Aug 19, 2017 at 19:07
  • @Psidom: they are numbers but i think it doesn't matter. Commented Aug 19, 2017 at 19:09

3 Answers 3

2

Your code seems fine except 1 line B=numpy.zeros(7,7) it should be B=numpy.zeros((7,7))

As per the documentation

A=[[1,0,0],[2,0,1],[3,0,2],
[4,1,0],[5,1,1],[6,1,2],
[7,2,0],[8,2,1],[9,2,2]]

import numpy as np

B = np.zeros((3,3))
for item in A:
    B[item[1]][item[2]]=item[0]

B

array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.],
       [ 7.,  8.,  9.]])

you can also do it in a simple way using reshape

np.array(A)[:,0].reshape(7,7)

How that works:

np.array(A)
array([[a_00, 0, 0],
       [a_01, 0, 1],
       [a_02, 0, 2],
       ...

np.array(A)[:,0]
array([a_00, a_01, a_02,...])

np.array(A)[:,0].reshape(3,3) # reshape it in the shape that we care for.
Sign up to request clarification or add additional context in comments.

2 Comments

I would index the 2d array B with: B[item[1], item[2]]
@hpaulj cool. share reason also so I can understand better, thanks :)
2

IIUC:

In [185]: a,b,c = zip(*A)

In [186]: np.array(a).reshape(np.unique(b).size, -1)
Out[186]:
array([['a_00', 'a_01', 'a_02', 'a_03'],
       ['a_10', 'a_11', 'a_12', 'a_13'],
       ['a_20', 'a_21', 'a_22', 'a_23'],
       ['a_30', 'a_31', 'a_32', 'a_33']],
      dtype='<U4')

Comments

2

The list is stored in the format of a sparse matrix, you can extract the value, row and column index separately and then construct a sparse matrix from it using scipy.sparse.coo_matrix:

lst = [[3,0,0],[2,0,1],[1,0,6],
       [5,1,0],[3,1,1],[2,1,6],
       [7,6,0],[5,6,1],[7,6,6]]

from scipy.sparse import coo_matrix

v, i, j = zip(*lst)
coo_matrix((v, (i, j)), shape=(7,7)).toarray()

#array([[3, 2, 0, 0, 0, 0, 1],
#       [5, 3, 0, 0, 0, 0, 2],
#       [0, 0, 0, 0, 0, 0, 0],
#       [0, 0, 0, 0, 0, 0, 0],
#       [0, 0, 0, 0, 0, 0, 0],
#       [0, 0, 0, 0, 0, 0, 0],
#       [7, 5, 0, 0, 0, 0, 7]])

Using @Vikash's data:

v, i, j = zip(*A)
coo_matrix((v, (i, j)), shape=(3,3)).toarray()
#array([[1, 2, 3],
#       [4, 5, 6],
#       [7, 8, 9]])

7 Comments

this is very elegant, but i believe OP wanted a different result set...
@MaxU Hmm. I just tested Vikash's data set as well. Seems to give the same result.
@Psidom: that's good but its shape is not desirable for my purpose.
@MaxU zeros are entries when the index is missing from the array.
@mahsa What shape do you need? I guess you can always specify the shape to your desired one by changing the shape parameter in coo_matrix, as long as the dimensions/shapes are larger than the index in the list.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.