|
 |
Kevin Wampler <wam### [at] u washington edu> wrote:
> Darren New wrote:
> > Actually, python dictionaries are pretty lame compared to dictionaries
> > in some other languages, like PHP.
>
> 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.
Yeah, me too. What is it lacking exactly? Specially compared to an aberration
like PHP?
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {}
>>> type( d )
<type 'dict'>
>>> d['foo'] ="bar"
>>> d
{'foo': 'bar'}
>>> dir( d )
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__',
'__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__gt__',
'__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__setitem__', '__str__', 'clear', 'copy', 'fromkeys', 'get', 'has_key',
'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem',
'setdefault', 'update', 'values']
>>> d.items()
[('foo', 'bar')]
>>> d[1] = 2
>>> d[1]
2
>>> d[2]=lambda x:x+1
>>> d
{1: 2, 2: <function <lambda> at 0x8198fb4>, 'foo': 'bar'}
>>> d[2](3)
4
>>> for k,v in d.items():
.... print k, " => ", v
....
1 => 2
2 => <function <lambda> at 0x8198fb4>
foo => bar
>>>
It can take arbitrary objects as keys and has a wealth of good methods to choose
from. Perhaps it's time for some friday night troll? ;)
Post a reply to this message
|
 |