[Tutor] How to store key->multiple values?
Michael Janssen
Janssen at rz.uni-frankfurt.de
Tue Dec 30 10:22:58 EST 2003
On Tue, 30 Dec 2003, Kenneth Gomez wrote:
> I would like some advice on how to store data of these types :
>
> Material Name Young's Modulus Poisson's Ratio Density
> Aluminum 200e5 .27 2700
> Steel 200e9 .33 8020
>
> I would like this to be in an array where I can access the first subscript
> and get the rest of the values.
>
> I thought of using a dictionary but I found that dictionary only allows one
> value per key. Can I use lists instead? How do I create a multidimensional
> list or tuple? How would I insert the values into a multidimensional list?
Lists and dictionaries can easily nest (Just another one of python's
strengths?). To do it store another list/dict as the value:
>>> d = {}
>>> d["Aluminum"] = {"Young's Modulus": 200e5,
... "Poisson's Ratio": .27,
... "Density": 2700,
... }
>>> d["Aluminum"]["Density"]
2700
perhaps it's easier to store the second level into a list (values are
then identified by list-index, eg d["Aluminum"][2] always shows
Density).
When your data gets more and more complicated, you might want to see how
it looks pretty:
>>> import pprint
>>> pprint.pprint(d)
[snip: pretty printed representation of d]
Michael
More information about the Tutor
mailing list