| Store | Cart

Q: What does "Sparse is better than dense" mean? (Python Zen)

From: Mike C. Fletcher <mcfl...@rogers.com>
Thu, 11 Jul 2002 16:23:59 -0400
Hmm, strangely, I'd always thought the "sparse is better than dense" 
line was refering to ideas of sparse versus dense data structures and/or 
programs.  For instance, if we have an 11-item sequence where we want to 
record which items are "important", we see two possibilities:

filterSet = [0,0,0,1,0,0,0,0,1,0,0,0]
filterSet = {3:1, 8:1}

and would choose the "sparse" matrix (the dict) instead of the "dense" one.

In a more general sense, record what is important explicitly, and where 
it is relevant, while not recording that which is unimportant to the 
matter at hand.

Or even more generally, make your intentions clear by coding just what 
you mean, not everything that could possibly be.  See the use of 
exceptions, which encourage you to code the "positive image" in a 
particular function, and allow a higher-level of code to deal with the 
outlier cases (the 0's in the array, which we don't really have anything 
to say about).

But, of course, I'm notoriously bad at channeling Tim, so the whitespace 
thing is probably it :) ,

> Miki Tebeka <tebeka at cs.bgu.ac.il> wrote:> >>Although it's in the Humor section I take the Python Zen>>(http://www.python.org/doc/Humor.html#zen) quite seriously.>>However I can understand what does “Sparse is better than>>dense” means.

Recent Messages in this Thread
Miki Tebeka Jul 11, 2002 11:28 am
Peter Hansen Jul 11, 2002 12:24 pm
Mark McEahern Jul 11, 2002 01:07 pm
Aahz Jul 11, 2002 06:35 pm
Mike C. Fletcher Jul 11, 2002 08:23 pm
Andrae Muys Jul 11, 2002 11:30 pm
jep...@unpythonic.net Jul 11, 2002 11:45 pm
Peter Hansen Jul 12, 2002 03:31 am
Ian Bicking Jul 12, 2002 08:00 am
Tim Peters Jul 12, 2002 08:22 am
Bernhard Herzog Jul 12, 2002 07:09 pm
Peter Hansen Jul 12, 2002 08:27 am
Messages in this thread