[Tutor] Truckers Log....Me Again
Erik Price
erikprice@mac.com
Tue, 22 Jan 2002 08:08:13 -0500
Paul,
Let me see if I can follow along (just learning this so this is good
exercise) ...
On Tuesday, January 22, 2002, at 04:29 AM, Paul Sidorsky wrote:
> (That might be a good learning project for
> those who were looking for one: implement a dictionary-like type using
> lists, entirely in Python.) For example:
>
> {"key1": 12, "key2": "hi", key3: [2, 4, 6], key4: (1, 2, 3)}
Here you've set up a dictionary.
> could be viewed as:
>
> [["key1", 12], ["key2", "hi"], ["key3", [2, 4, 6]], ["key4", (1, 2, 3)]]
At this point, is it still a dictionary? Or have you performed some
operation to make it a list? I see the square brackets, which if I
recall correctly, surround lists.
> or also this way, which is probably closer to how Python dicts actually
> work:
>
> keys = ["key1", "key2", "key3", "key4"]
> values = [12, "hi", [2, 4, 6], (1, 2, 3)]
Two separate lists.
> A big problem with using lists is, of course, that access and
> manipulation starts to get tricky. Another problem is you now have to
> search to find keys; this may involve traversing most of the list.
> Dictionaries use a hashtable so there is no search time involved (well
> except with chaining, but that's normally not a significant factor).
>
> The concept of subscripting with, say, a string (e.g. mydict["key1"])
> may seem unusual at first, but it does become natural! Subscripting
> with a more complex object like a tuple or a class is even more bizzare
> the first time you do it but it does save tons of work!
I'm not sure that I follow you correctly -- I see what you're saying in
the last paragraph but I haven't put together how it applies to the
examples above.
Especially confusing (to me) is how Python allows you to define a
dictionary, but then split that up into a variable "keys" and a variable
"values", as shown above. But then, it's possible that the reference I
used hasn't gotten that far yet.
Erik