Summing a 2D list
maric at aristote.info
Fri Jun 13 18:55:24 CEST 2008
Le Friday 13 June 2008 17:55:44 Karsten Heymann, vous avez écrit :
> Maric Michaud <maric at aristote.info> writes:
> > So, writing C in python, which has dictionnary as builtin type,
> > should be considered "more elegant" ?
> IMO that's a bit harsh.
harsh ? Sorry, I'm not sure to understand.
> > You are comparing apples with lemons, there is no such a difference
> > between list index access and dictionnary key access in Python.
> > If you know in advance the number and names of users, what prevent
> > you to initialize completelly the target dictionnary ?
> > The following code compare the same algorithm, once with list and
> > the second time with dict :
> > The result is pretty close now :
> > maric at redflag1 17:04:36:~$ ./test.py
> > with list 1.40726399422
> > with dict 1.63094091415
> > So why use list where the obvious and natural data structure is a
> > dictionnary ?
> I'd never argue that using a dictionary is the obvious and natural
> data structure for this case. But is it the best? Honestly, as your
> very nice example shows, we have two solutions that are equally fast,
> equally complex to code and equally robust, but one needs
Yes, but my example take ordered integer for keys (users' names) which they
should not be in a real case, so retrieving the result is by way easier (and
faster) with a dictionnary.
> approximately the double amount of memory compared to the other.
I don't see how you came to this conclusion. Are you sure the extra list take
twice more memory than the extra dictionary ?
> So, as much as i like dictionaries, what's the gain you get from using it
> in this corner case?
It's the very purpose of it's usage, store and retrieve data by key.
More information about the Python-list