Creating dynamically named lists/dictionaries
roy at panix.com
Thu Apr 24 01:38:22 CEST 2003
"Rogue 9" <rogue9 at ntlworld.com> wrote:
> Now what I've ended up with is a list containing one draw result
> for each week (768 at the last count)as individual items in that list e.g
> list =[[767,11,22,33,44,45,46]......[1,20,33,41,45,46,47]]
So far, this seems pretty reasonable.
> What I really want is a list or dictionary for each individual weeks
> result but I can't seem to work out how to dynamically create a list or
> dict with a unique name for each week.
The right way to think of both lists and dictionaries is that they are
maps. Not maps in the www.mapquest.com sense, but maps in the
mathematical sense. For example, let's say we were talking about the
suits in a deck of cards. I could make a list of them:
suits = ['clubs', 'diamonds', 'hearts', 'spades']
and now I can talk about mapping the integers 0, 1, 2, and 3 to the suit
names 'clubs', 'diamonds', 'hearts', and 'spades'. I give you a key,
and you can give me back a value.
The difference between lists and dictionaries is that the keys in lists
are restricted to be integers, while the keys in dictionaries can be
either integers or strings (or other things, but for the sake of this
discussion, integers and strings are all we need to worry about).
I don't know what you want to do with the data, but the list of lists
you've already got seems like a pretty reasonable data structure to me.
The keys seem like they should be integers, since you're talking about
week #1, week #2, and so on. You can already map weeks to winning
numbers. If I said, "what were the numbers for week 17?", you would
just have to get list (keeping in mind that lists are indexed
starting from 0; so you might really want to get list).
What is it that you want to do with the data that make you think you
want anything more complex than what you've got? You are right that
creating hundreds of individually named lists or dictionaries would be a
problem, but it's almost certainly not what you want to do.
More information about the Python-list