|
 |
Darren New wrote:
> Kevin Wampler wrote:
>> I don't know PHP, so I'm curious if you could elaborate a bit, since
>> I'm used to viewing "dictionary" as more of less a synonym for
>> "hashtable" in this context.
>
> Hmmm... A dictionary in PHP is more like an array of key/value pairs. So
> they preserve the order in which you inserted elements
Interesting. So, to make sure I understand correctly if dicts in python
were to operate like this then:
D = dict()
d["a"] = "foo"
d["b"] = "bar"
d["c"] = "baz"
would give me {"a":"foo", "b":"bar", "c":"baz"} where the order matters
so D[1] gives "bar"?
If so, then what happens in the following case:
D = dict()
d[3] = "foo"
d[2] = "bar"
d[1] = "baz"
or:
D = dict()
D[2000000000] = "foo"
I think any implementation which tries to behave both like a list and a
hashtable is going to have some inconsistencies when using ints as keys,
so I actually prefer the approach of keeping the two separate. That
said I do sometimes like the C++ stl approach of maintaining an ordering
in a map based on the natural order of its elements.
> Honestly, I don't think I've ever *used* the fact that the keys maintain
> an order even when they're not integers.
I actually have used this in python a few times, using a custom class
rather than a straight dict of course. Unfortunately I can't remember
why I needed it. I suspect it was so that I could pack the dict into a
vector, perform some computations on it, and then put the results back
into the dict.
Post a reply to this message
|
 |