[Tutor] Two dimesional array

kinuthiA muchanE muchanek at gmail.com
Thu Jun 19 09:39:08 CEST 2008


On Thu, 2008-06-19 at 02:30 +0200, tutor-request at python.org wrote:
> Message: 2
> Date: Wed, 18 Jun 2008 19:43:21 +0530
> From: "amit sethi" <amit.pureenergy at gmail.com>
> Subject: [Tutor] (no subject)
> To: tutor at python.org
> Message-ID:
>         <da81a0a80806180713x6b0641d4sc4f6d2d36b5bcdd2 at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Hi , Could you please tell me , how i can traverse a two dimensional
>>> 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
> 
> Does this help?
> Kinuthia...





More information about the Tutor mailing list