Dynamically growing an array to implement a stack
M. Hamed
mhelshou at hotmail.com
Thu Apr 8 16:21:57 EDT 2010
I have trouble with some Python concept. The fact that you can not
assign to a non-existent index in an array. For example:
a = [0,1]
a[2] =========> Generates an error
I can use a.append(2) but that always appends to the end. Sometimes I
want to use this array as a stack and hence my indexing logic would be
something like:
If you are already at the end (based on your stack pointer):
use append() then index (and inc your pointer)
if not:
index directly (and inc your stack pointer)
If feel that doing this everytime I want to add an element that I have
to check whether it exists or not is too much. Is there any simpler
way to do this?
I know I can do something like this:
a = numpy.zeros(MAX_STACK_SIZE)
but I don't want to predetermine the stack size.
Any help?
Thanks
More information about the Python-list
mailing list