[Tutor] Dictionaries
John Fouhy
john at fouhy.net
Thu Jan 26 02:00:28 CET 2006
On 26/01/06, Jon Moore <jonathan.r.moore at gmail.com> wrote:
> Hi
>
> Is there anyway to print informtation from dictionaries better than this?:
Well, you can print the value too if you want :-)
eg:
>>> pairs = {"Jon Moore": "Tony Moore",
... "Simon Nightingale": "John Nightingale",
... "David Willett": "Bernard Willet",
... "John Jackson": "Stuart Jackson",
... "James Southey": "Richard Southey",
... "William Forsythe": "Shaun Forsythe"}
>>> for name in pairs:
... print name, pairs[name]
...
David Willett Bernard Willet
Jon Moore Tony Moore
John Jackson Stuart Jackson
Simon Nightingale John Nightingale
James Southey Richard Southey
William Forsythe Shaun Forsythe
That is a bit ugly. We could at least put something between the names
so we know where one name ends and the next begins!
>>> for name in pairs:
... print name, '::', pairs[name]
...
David Willett :: Bernard Willet
Jon Moore :: Tony Moore
John Jackson :: Stuart Jackson
Simon Nightingale :: John Nightingale
James Southey :: Richard Southey
William Forsythe :: Shaun Forsythe
We could have achieved the same thing by using string formatting
operations --- see
http://python.org/doc/2.4.2/lib/typesseq-strings.html for details.
You'll want to have at least some familiarity with them if you are
doing any kind of string building!
In this case, the example looks like this:
>>> for name in pairs:
... print '%s :: %s' % (name, pairs[name])
...
David Willett :: Bernard Willet
Jon Moore :: Tony Moore
John Jackson :: Stuart Jackson
Simon Nightingale :: John Nightingale
James Southey :: Richard Southey
William Forsythe :: Shaun Forsythe
Of course, that doesn't line up the two columns. If you want to do
that, the first thing you'll need to do is figure out how wide the
columns need to be --- which means looking through all the key/value
pairs and seeing how long each one is.
We could do that like this:
>>> maxKey, maxValue = 0, 0
>>> for name in pairs:
... if len(name) > maxKey:
... maxKey = len(name)
... if len(pairs[name]) > maxValue:
... maxValue = len(pairs[name])
...
>>> maxKey, maxValue
(17, 16)
(you could also do it in one line with a clever list comprehension,
but that's a bit more opaque [1])
Once we've got those, we can work out how much padding we need for each name.
So, we could do this:
>>> for name in pairs:
... print name, ' '*(maxKey-len(name)), pairs[name], '
'*(maxValue-len(pairs[name]))
...
David Willett Bernard Willet
Jon Moore Tony Moore
John Jackson Stuart Jackson
Simon Nightingale John Nightingale
James Southey Richard Southey
William Forsythe Shaun Forsythe
(did you know that you can multiply strings? '.'*5 == '.....')
Using a comma in a print statement adds an extra space, so it might be
a bit tricky getting your formatting right. If we use string
formatting operations, it's a lot easier:
>>> for name in pairs:
... print '%*s --> %*s' % (maxKey, name, maxValue, pairs[name])
...
David Willett --> Bernard Willet
Jon Moore --> Tony Moore
John Jackson --> Stuart Jackson
Simon Nightingale --> John Nightingale
James Southey --> Richard Southey
William Forsythe --> Shaun Forsythe
Hmm, everything's right-justified. We can left-justify by adding a
'-' to the length field in the string.
>>> for name in pairs:
... print '%*s --> %-*s' % (maxKey, name, maxValue, pairs[name])
...
David Willett --> Bernard Willet
Jon Moore --> Tony Moore
John Jackson --> Stuart Jackson
Simon Nightingale --> John Nightingale
James Southey --> Richard Southey
William Forsythe --> Shaun Forsythe
HTH!
--
John.
[1] Ok, I couldn't resist. Here it is:
>>> mK, mV = [max(x) for x in zip(*[(len(a), len(b)) for a, b in
pairs.items()])]
>>> mK, mV
(17, 16)
I guess that's technically two list comprehensions, but it does only
iterate through pairs once.
More information about the Tutor
mailing list