Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35774

Re: dict comprehension question.

From Peter Otten <__peter__@web.de>
Subject Re: dict comprehension question.
Date 2012-12-29 21:32 +0100
Organization None
References <724d4fea-606a-4503-b538-87442f6bcebc@ci3g2000vbb.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1440.1356813160.29569.python-list@python.org> (permalink)

Show all headers | View raw


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})

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

dict comprehension question. Quint Rankid <qbr567@gmail.com> - 2012-12-29 11:48 -0800
  Re: dict comprehension question. Roy Smith <roy@panix.com> - 2012-12-29 14:58 -0500
  Re: dict comprehension question. Mitya Sirenef <msirenef@lightbird.net> - 2012-12-29 15:01 -0500
  Re: dict comprehension question. Mitya Sirenef <msirenef@lightbird.net> - 2012-12-29 15:09 -0500
  Re: dict comprehension question. Joel Goldstick <joel.goldstick@gmail.com> - 2012-12-29 15:15 -0500
  Re: dict comprehension question. Peter Otten <__peter__@web.de> - 2012-12-29 21:32 +0100
  Re: dict comprehension question. MRAB <python@mrabarnett.plus.com> - 2012-12-29 20:39 +0000
  Re: dict comprehension question. Mitya Sirenef <msirenef@lightbird.net> - 2012-12-29 16:40 -0500
  Re: dict comprehension question. Terry Reedy <tjreedy@udel.edu> - 2012-12-29 18:22 -0500
  Re: dict comprehension question. Terry Reedy <tjreedy@udel.edu> - 2012-12-29 18:56 -0500
    Re: dict comprehension question. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-01 03:10 +0000
      Re: dict comprehension question. 88888 Dihedral <dihedral88888@googlemail.com> - 2013-01-01 00:40 -0800
  Re: dict comprehension question. Tim Chase <python.list@tim.thechases.com> - 2012-12-29 18:26 -0600
  Re: dict comprehension question. Joel Goldstick <joel.goldstick@gmail.com> - 2012-12-29 23:10 -0500

csiph-web