followup: vertical slices from a matrix in a dictionary?...

mary mary.stern at virgin.net
Wed Apr 25 10:00:35 EDT 2001


Well I found the answer to my previous post (in the excellent
'Essential Python Reference' book by David M Beazley - highly recommended),
but now I have a follow-up question.

I can use 'map(None, a, b, c)' to take 'vertical slices out of tuples a, b
and c.
I want to generalise this to a dictionary of tuples. I'm a bit stuck on
how to call map with a varying number of arguments, ie:

Here's a program which works as desired, but is not generalised:
---------------------------------
x = {}
x['gamma'] = (1,2,3)
x['beta']  = (4,5,6)
x['alpha'] = (7,8,9)
print x
print x.keys()
# --- sort keys
y = x.keys()
y.sort()
print y
# --- take vertical slices of x (in y-order)
z = map(None, x[y[0]], x[y[1]], x[y[2]])                <---How do I
generalise this call?...
print z
----------------------------------------------------
But I want to take a dictionary with any number of keys, sort
them into alphabetica order, and then get the 'vertical slices'
in alphabetival order (ie how do I avoid hardcoding the x[y[0], x[y[1]],
etc...)

Any help appreciated! :)
- Tushar





More information about the Python-list mailing list