From d2ca53f7f29b4ba6c55c6ad632171a3ead8ddbfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bahad=C4=B1r=20Kandemir?= Date: Sun, 9 Apr 2006 21:04:43 +0000 Subject: [PATCH] Python Performance Tips - Initializing Dictionary Elements: Dictionaries now have a get() method which will return a default value if the desired key isn't found in the dictionary. This simplifies the loop: wdict = {} get = wdict.get for word in words: wdict[word] = get(word, 0) + 1 --- tools/repostats.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tools/repostats.py b/tools/repostats.py index 073ab6b5..b51ef527 100755 --- a/tools/repostats.py +++ b/tools/repostats.py @@ -278,10 +278,7 @@ class Histogram: if value: self.list[name] = value else: - if self.list.has_key(name): - self.list[name] = self.list[name] + 1 - else: - self.list[name] = 1 + self.list[name] = self.list.get(name, 0) + 1 def note(self, name): if not self.list.has_key(name):