Re: Check for dict key existence, and modify it in one step.
- From: Evan Klitzke <evan@xxxxxxxx>
- Date: Tue, 28 Aug 2007 08:43:12 -0700
On Tue, 2007-08-28 at 14:36 +0000, rodrigo wrote:
Im using this construct a lot:
if dict.has_key(whatever):
dict[whatever] += delta
else:
dict[whatever] = 1
In Python 2.5 there's a defaultdict class, otherwise you can subclass
dict like this:
class CountingDictionary(dict):
def increment(self, key, delta=1):
self[key] = self.get(key, 0) + delta
Hope this helps!
--
Evan Klitzke <evan@xxxxxxxx>
.
- Follow-Ups:
- References:
- Check for dict key existence, and modify it in one step.
- From: rodrigo
- Check for dict key existence, and modify it in one step.
- Prev by Date: in design time activeX properties
- Next by Date: Re: int/long bug in locale?
- Previous by thread: Re: Check for dict key existence, and modify it in one step.
- Next by thread: Re: Check for dict key existence, and modify it in one step.
- Index(es):
Relevant Pages
|