Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'messages.': 0.04; 'newbie': 0.05; 'result,': 0.05; 'subject:skip:c 10': 0.07; 'python': 0.09; '[1,': 0.09; 'closest': 0.09; 'collections': 0.09; 'dict': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'looked': 0.10; 'bind': 0.16; 'googled': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:question.': 0.16; 'value:': 0.16; 'var.': 0.16; 'wanted.': 0.16; 'wrote:': 0.17; 'instance': 0.17; "shouldn't": 0.17; '>>>': 0.18; 'variable': 0.20; 'question.': 0.20; 'trying': 0.21; 'import': 0.21; 'not,': 0.21; "haven't": 0.23; "i've": 0.23; 'tried': 0.25; 'header:User- Agent:1': 0.26; 'question': 0.27; 'to?': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'probably': 0.29; "i'm": 0.29; 'error': 0.30; 'gets': 0.32; 'like:': 0.33; 'to:addr:python-list': 0.33; 'version': 0.34; 'list': 0.35; 'clear': 0.35; 'doing': 0.35; 'received:org': 0.36; 'but': 0.36; 'skip:{ 10': 0.36; 'method': 0.36; 'should': 0.36; 'possible': 0.37; 'does': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'supports': 0.38; 'things': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'most': 0.61; 'side': 0.61; 'results': 0.65; 'beat': 0.65; 'answer.': 0.71; 'obvious': 0.71; 'dict.': 0.84; 'spell': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: dict comprehension question. Date: Sat, 29 Dec 2012 21:32:29 +0100 Organization: None References: <724d4fea-606a-4503-b538-87442f6bcebc@ci3g2000vbb.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849898.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356813160 news.xs4all.nl 6911 [2001:888:2000:d::a6]:42542 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35774 Quint Rankid wrote: > Newbie question. I've googled a little and haven't found the answer. > > Given a list like: > w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] > I would like to be able to do the following as a dict comprehension. > a = {} > for x in w: > a[x] = a.get(x,0) + 1 > results in a having the value: > {1: 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1} > > I've tried a few things > eg > a1 = {x:self.get(x,0)+1 for x in w} > results in error messages. > > And > a2 = {x:a2.get(x,0)+1 for x in w} > also results in error messages. > > Trying to set a variable to a dict before doing the comprehension > a3 = {} > a3 = {x:a3.get(x,0)+1 for x in w} > gets this result, which isn't what I wanted. > {1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1} > > I'm not sure that it's possible to do this, and if not, perhaps the > most obvious question is what instance does the get method bind to? The name a3 will not be rebound until after the right side is evaluate. To spell it with a loop: a3 = {} _internal = {} # You have no access to this var. # Even if you can beat a particular # Python implementation -- you shouldn't for x in w: _internal[x] = a3.get(x, 0) + 1 a3 = _internal That should make it clear that x will be looked up in the "old" empty a3 dict. The closest you can get to a self-updating dict is probably >>> w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] >>> a1 = {} >>> a1.update((x, a1.get(x, 0)+1) for x in w) >>> a1 {1: 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1} but that it works doesn't mean it is a good idea. If your Python version supports it the obvious choice is >>> from collections import Counter >>> w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] >>> Counter(w) Counter({1: 3, 2: 2, 4: 2, 3: 1, 5: 1, 6: 1})