[Tutor] Help me with two dimensional arrays in Python

Asrarahmed Kadri ajkadri at googlemail.com
Thu Oct 5 13:01:12 CEST 2006


Its something very close.
What I want is:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

This can be done using arrays, right or is there any other way??
My logic in C is:

int arr[5][5];
for (i=0;i<5;i++)
{
   flag = 0;
   for (j=0;j<=i;j++)
{
   if (flag == 0 || j == i)  // print 1 if the array element is the first or
the last number
     {
      arr[i][j] = 1;
      flag = 1;
    }
  arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
}

thanks.



On 10/4/06, Matthew White <mwhite3 at ttsd.k12.or.us> wrote:
>
> Asrarahmed,
>
> How about something like this:
>
> >>> arr = []
> >>> arr.append(['Blue', 'Yellow', 'Green', 'Brown', 'White'])
> >>> arr[0][0]
> 'Blue'
> >>> arr.append(['Up', 'Down', 'Left', 'Right', 'Center'])
> >>> arr[1][4]
> 'Center'
>
> Is this what you were thinking of?
>
> -mtw
>
>
> On Wed, Oct 04, 2006 at 06:38:09PM +0100, Asrarahmed Kadri (
> ajkadri at googlemail.com) wrote:
> > I am looking for something like this:
> >
> > int arr[5][5]; // C statement; declaring an array of 5 * 5 size
> >
> > Is there an easy and clean way to achieve this in python???
> >
> > Thanks
> >
> >
> >
> > On 10/4/06, Kent Johnson <kent37 at tds.net> wrote:
> > >
> > >Asrarahmed Kadri wrote:
> > >>
> > >> Hi folks,
> > >>
> > >> I am stuck.. Please help me with implementing two dimensional array
> in
> > >> Python.
> > >
> > >Nested lists are the simplest way though beware the gotcha shown here:
> > >
> > >
> http://www.python.org/doc/faq/programming/#how-do-i-create-a-multidimensional-list
> > >
> > >You can also use a dict with tuple indices, e.g.
> > >d[1,2] = 3
> > >
> > >Numpy is an extension designed for high-performance numerical work, it
> > >includes a multidimensional array type:
> > >http://numpy.scipy.org//
> > >
> > >Kent
> > >
> > >
> >
> >
> > --
> > To HIM you shall return.
>
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
To HIM you shall return.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061005/e68595ba/attachment.html 


More information about the Tutor mailing list