On Sun, 2008-06-29 at 10:20 +0200, [EMAIL PROTECTED] wrote:
> Message: 1
> Date: Sat, 28 Jun 2008 21:53:44 -0400
> From: Kirk Z Bailey <[EMAIL PROTECTED]>
> Subject: [Tutor] arrays in python
> To: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 
> Just wondering, if I can find a way to do a 2 dimensional array in 
> python. 1 dimension would be a list it would seem; for 2, I could use
> a 
> list of lists?
> 
> Strange how I can't think of ever needing one since I discovered
> snake 
> charming, but so many languages do foo dimensional arrays, it would
> seem 
> like there ought to be a way to do it in python.
> 
> -- 

>>> from numpy import *  # import the necessary module

>>> arry = array((1,2,3,4)) # create a rank-one array
>>> print arry
[1 2 3 4]
>>> print arry.shape
(4,) # this means it is a rank 1 array with a length of 4  (the trailing
comma means it is a tuple)

To get to the first element in the array:
>>> print arry[0]
1
 
 To get to the last element:
>>> print arry[-1]
 4
>>> arry2 = array(([5,6,7,8],[9,10,11,12])) # create a a rank-two array,
two-dimensional, if wish
>>> print arry2
 [[ 5  6  7  8]
  [ 9 10 11 12]]
>>> print arry2.shape #
> (2, 4) # this means that it is a rank 2 (ie 2-dimensional) array, with
each axis having a length of 4
>>> 
 To get to the first element in the first axis:
>>> print arry2[0,0]
 5
To get to the last element in the second axis:
>>>> print arry2[1,-1]
 12

You can slice it, reshape it, literally you can contort in way you want!
 
 Does this help?
 Kinuthia...


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to