Re: [Tutor] Help me with two dimensional arrays in Python

2006-10-09 Thread János Juhász
Dear Asrarahmed,

I have found the next recipe in the cookbook.
It should be interesting for you :)

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/392153

 Message: 1
 Date: Thu, 05 Oct 2006 10:26:19 -0400
 From: Kent Johnson [EMAIL PROTECTED]
 Subject: Re: [Tutor] Help me with two dimensional arrays in Python
 Cc: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed

 Asrarahmed Kadri wrote:
  Its something very close.
  What I want is:
 
  1
  1 1
  1 2 1
  1 3 3 1
  1 4 6 4 1



Yours sincerely, 
__
János Juhász 

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


Re: [Tutor] Help me with two dimensional arrays in Python

2006-10-05 Thread Asrarahmed Kadri
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;i5;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 [EMAIL PROTECTED] 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?-mtwOn Wed, Oct 04, 2006 at 06:38:09PM +0100, Asrarahmed Kadri (
[EMAIL PROTECTED]) 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 [EMAIL PROTECTED] 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@python.org http://mail.python.org/mailman/listinfo/tutor
-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me with two dimensional arrays in Python

2006-10-05 Thread Luke Paireepinart
Asrarahmed Kadri wrote:
 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??
They're not arrays, they're lists!
arrays imply contiguous memory blocks and single data types.
Lists are neither of these.
 My logic in C is:
  
 int arr[5][5];
 for (i=0;i5;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];
 }
Yes, you could do this in Python.
I'm guessing it'd be something like this:

lst = []
for x in range(5):
tmp = []
for y in range(x):
   if y == 0:
  tmp.append(1)
   else:
  tmp.append(tmp[y-1]) #this line would be different
lst.append(tmp)

That's the best I can do.

HTH,
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me with two dimensional arrays in Python

2006-10-05 Thread Asrarahmed Kadri
Thank you so much.
I am trying to implement. I hope I can do it.

A gentle note: This is not a homework question.
I am learning python as a part of my Project work.

On 10/5/06, Luke Paireepinart [EMAIL PROTECTED] wrote:
Asrarahmed Kadri wrote: 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??They're not arrays, they're lists!arrays imply contiguous memory blocks and single data types.
Lists are neither of these. My logic in C is: int arr[5][5]; for (i=0;i5;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]; }Yes, you could do this in Python.I'm guessing it'd be something like this:
lst = []for x in range(5): tmp = [] for y in range(x):if y == 0: tmp.append(1)else: tmp.append(tmp[y-1]) #this line would be different lst.append(tmp)
That's the best I can do.HTH,-Luke-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me with two dimensional arrays in Python

2006-10-05 Thread Kent Johnson
Asrarahmed Kadri wrote:
 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??

Is this a homework assignment?

A list of lists seems like the appropriate way to represent this, 
especially since it is not actually a square array.

Here is a simpler example that might give you some ideas:
In [5]: b=[]

In [6]: for i in range(5):
...: b.append(range(i))
...:
...:

In [7]: b
Out[7]: [[], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3]]

Kent

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


Re: [Tutor] Help me with two dimensional arrays in Python

2006-10-04 Thread Luke Paireepinart
Asrarahmed Kadri wrote:

 Hi folks,
  
 I am stuck.. Please help me with implementing two dimensional array in 
 Python.
  
There are no such things as arrays in Python, silly!
I can help you with a two-dimensional list, though :)


Okay, imagine that a list of integers (or any other objects) is a single 
object itself,
that keeps track of the integers (or other objects) for you in whatever 
order you want.
somelist = [1,2,3,4,5]
anotherlist = [6,7,8,9,10]

Now what you want out of a two-dimensional list is
where there are multiple inner lists, with each individual
list being a full row, right?
so...
two_dimensional = [somelist,anotherlist]

Or:
two_dimensional = [ [1,2,3,4,5] , [6,7,8,9,10] ]

This isn't a homework question, right?

Good luck,
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me with two dimensional arrays in Python

2006-10-04 Thread Kent Johnson
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

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


Re: [Tutor] Help me with two dimensional arrays in Python

2006-10-04 Thread Kent Johnson
Asrarahmed Kadri 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???

Can you say something about your use case? If I had to guess I would say 
numpy is what you are looking for. Did you look at my suggestions? Are 
you a Python beginner?

Kent

  
 Thanks
 
 
  
 On 10/4/06, *Kent Johnson* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] 
 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
 
 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@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me with two dimensional arrays in Python

2006-10-04 Thread Danny Yoo

 I am stuck.. Please help me with implementing two dimensional array 
 in Python.

Hi Asrarahmed,

What do you need a two-dimensional array for?  This is a serious question: 
what's your application?  What are you trying to represent, and why?


As a glib possible answer:  you can use a dictionary to map 2-tuples to 
values.  If you think about it, that's almost what a 2d array does in 
other languages.

##
points = {}
points[(3, 4)] = 1
points[(1, 7)] = 2
##

But this may not be the right approach for your problem.  Tell us more of 
what you are trying to do.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor