Create 2D character matrix

arsyed arsyed at gmail.com
Fri Aug 8 06:22:23 EDT 2008


On Thu, Aug 7, 2008 at 1:36 PM, Simon Parker <simon_ecc at yahoo.co.uk> wrote:
> Hello.
>
> I want to be able to create a 2D character matrix, ThisMatrix, like :
>
>
> a     A
> b     B
> c     C
> d     D
>
> and to be able to pick out elements, or rows or columns.
>
> I have become used to programming in R where I can easily refer to a row as
> :
>
> ThisMatrix [1,]
>
> and a column as
>
> ThisMatrix[,1].
>
> etc..
>
> Can this be done easily in Python ?
>
>

You can use numpy:

http://www.scipy.org/NumPy

for example:

In [139]: arr = numpy.char.array(['a', 'A', 'b', 'B', 'c', 'C'])

In [140]: arr = arr.reshape((3,2))

In [141]: arr
Out[141]:
chararray([['a', 'A'],
       ['b', 'B'],
       ['c', 'C']],
      dtype='|S1')

In [142]: arr[0,:]
Out[142]:
chararray(['a', 'A'],
      dtype='|S1')

In [143]: arr[:,0]
Out[143]:
chararray(['a', 'b', 'c'],
      dtype='|S1')

More documentation here:

http://mentat.za.net/numpy/refguide/

or take a look at these pages for some quick examples of what's possible:

http://pages.physics.cornell.edu/~myers/teaching/ComputationalMethods/python/arrays.html
http://www.scipy.org/Numpy_Example_List_With_Doc



More information about the Python-list mailing list