Many newbie questions regarding python

Tim Harig usernet at ilthio.net
Fri Oct 8 12:49:11 EDT 2010


On 2010-10-08, BartC <bc at freeuk.com> wrote:
> "Rogério Brito" <rbrito at ime.usp.br> wrote in message 
> news:i8lk0n$g38$1 at speranza.aioe.org...
>> If possible, I would like to simply declare the list and fill it latter in 
>> my
>> program, as lazily as possible (this happens notoriously when one is using 
>> a
>> technique of programming called dynamic programming where initializing all
>> positions of a table may take too much time in comparison to the filling 
>> of the
>> array).
>
> A sparse array? Even if an array could be constructed by assigning to 

I agree, what the OP seems to want (whether he actually needs it or not) is
a sparse array.

> A sparse array? Even if an array could be constructed by assigning to 
> arbitrary elements, the gaps created would still need filling in with None 
> or Unassigned.

That is only true when attempting to address elements based on their
position rather then something like rank.  You could create an object
with some kind of index attribute that you could search for built in.
When you needed to access the element, you simply walked the list until
you find the matching identifier.  The spaces no longer need filling since
you are not matching based on absolute position in the list.  This is of
course inefficient for large lists, and while there are several methods
of making this method more efficient (indexing, skip linking, b-tree,
etc), you can get what you want very efficiently most of the time by
simply using the built in dictionary with integer keys instead of a list.



More information about the Python-list mailing list