Multidimensional Dictionaries: Possible?
bjorn
bjorn at roguewave.com
Tue Jan 25 13:29:16 EST 2000
> What's the quickest way to do the equivalent of the following Perl in Python,
> again, using the above example:
>
> $dict{$key1}{$key2}++
>
> I can only think of the following, I hope there is a better way! :
>
> a = dict[key1, key2]
> a = a + 1
> dict[key1, key2] = a
How about:
class Counter:
def __init__(self, n):
self.n = n
def inc(self):
self.n = self.n + 1
return self.n
dict[key1, key2] = Counter(0)
dict[key1, key2].inc()
-- bjorn
More information about the Python-list
mailing list