Re: [Tutor] declaring list

2006-01-11 Thread Logesh Pillay
hello list

I asked yesterday how we can "declare" an array in python.  Thanks Brian
van den Broek and Frank Schley for your responses.

A[None] * n works for me.

To answer Brian's question, I was writing the standard backtracking
procedure to find the permutations of 1..n taken r at a time.

def permutations (n, r):
A, Used = [None] * (r+1), [False] * (n+1)
def choose (m):
if m > r:
print A[1:],
else:
for j in range (1, n+1):
if not Used [j]:
Used [j] = True
A[m] = j
choose (m+1)
Used [j] = False
choose (1)

Thanks 
Logesh Pillay

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


Re: [Tutor] declaring list in python

2006-01-10 Thread Brian van den Broek
Logesh Pillay said unto the world upon 10/01/06 11:28 PM:
 > Hello list
 >
 > I want to declare a list of a specific size as global to some nested
 > function like so

Hi Logesh,

what problem are you trying to solve by doing this? Knowing that will 
help generate more useful answers, I suspect.


 > def foo (n):
 >  A[] (of size n)
 >  def foo1
 >  ...
 >
 > The only way I can think to declare the list is to use dummy values:
 >  A = [0] * n
 >
 > A = [] * n doesn't work.  [] * n = []

A = [None] * n

would be a better way to created an n-placed list of dummy values in 
Python, I think.


 > I'd prefer not to use dummy values I have no use for.  Is there any 
way?

This is why knowing your problem would be helpful. Built-in Python 
data structures don't have size limitations that are declared when an 
instance of the data structure is created. (Python's not C.) There is 
no way (that I know of) to create an n-placed list save creating a 
list with n objects.

So, I think you will have to either give up on not employing dummy 
values or give up on creating a list of a fixed length.

You could subclass list to create a class with a max. and/or min. 
length, but I think knowing more about what you want to do would be 
helpful before getting into that :-)

Best,

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