[Tutor] sample dictionairies
Steven D'Aprano
steve at pearwood.info
Sun Apr 19 12:42:46 CEST 2015
On Sat, Apr 18, 2015 at 06:56:41PM -0700, Jim Mooney wrote:
> Where could I download Python sample dictionaries on different subjects.
> They're hard to type and I can only do small, limited ones to practice with.
I don't think you can download Python dicts. It would be a bit hard,
since Python dicts exist only while the Python interpreter is running.
Normally data is provided in text form, and then read from a file and
converted into a dict or list as needed.
If you search the internet, you will find lots of places that you can
download data on various topics. You can start at Wikidata:
https://www.wikidata.org/
http://en.wikipedia.org/wiki/Wikidata
But for simple experiments, there is an easier solution. Instead of
typing the dicts yourself, create them programmatically!
Let's say I want a dict like:
{'a': 1, 'b': 2, 'c': 3, ... 'z': 26}
That would be a PITA to type out in full. But I can do this:
import string
pairs = zip(string.ascii_lowercase, range(1, 27))
d = dict(pairs)
zip is invaluable for this sort of thing, because it takes two (or more)
independent sequences and "zips" them together into a single sequence of
pairs. So zip('abcde...z', [1, 2, 3, 4, 5, ..., 26]) ends up as the
sequence [('a', 1), ('b', 2), ('c', 3), ... ] which is exactly what
dict() needs to build a dictionary.
Here's another example:
from random import random
d = dict((n, random()) for n in range(10000))
This uses a *generator expression* to provide a sequence of (key, value)
pairs, where the keys are numbers 0, 1, 2, ... and the values are random
floats. You might have seen list comprehensions:
[x**2+1 for x in range(15)]
for example. Generator expressions are similar, except they don't
calculate the items up front into a list, they calculate them lazily
only when needed. To turn a list comprehension into a generator
expression, change the outer-most square brackets [ ] into round
brackets:
(x**2+1 for x in range(15))
In Python 3, we also have *dict comprehensions* which are based on list
comprehensions:
d = {n: random() for n in range(10000)}
So my advice is this: rather than look for somebody to provide you with
ready-made dicts, learn how to assemble them yourself!
# dict of {name: age} pairs
names = 'Fred Wilma Barney Betty Homer Marge'.split()
ages = [35, 31, 34, 36, 30]
d = dict(zip(name, ages))
--
Steve
More information about the Tutor
mailing list