Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add an additional row and column to an array?

Tags:

python

I need to add a column and a row to an existing Numpy array at a defined position.

like image 618
ricardo Avatar asked Jan 04 '10 21:01

ricardo


1 Answers

I assume your column and rows are just a list of lists?

That is, you have the following?

L = [[1,2,3],
     [4,5,6]]

To add another row, use the append method of a list.

L.append([7,8,9])

giving

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

To add another column, you would have to loop over each row. An easy way to do this is with a list comprehension.

L = [x + [0] for x in L]

giving

L = [[1,2,3,0],
     [4,5,6,0]]
like image 154
Stefan Kendall Avatar answered Sep 22 '22 20:09

Stefan Kendall