Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'skip:[ 20': 0.03; 'messages.': 0.04; 'newbie': 0.05; 'result,': 0.05; 'item.': 0.07; 'subject:skip:c 10': 0.07; '[1,': 0.09; 'compact': 0.09; 'dict': 0.09; 'python:': 0.09; 'cc:addr:python-list': 0.10; 'dec': 0.15; 'sat,': 0.15; '>>': 0.16; 'bind': 0.16; 'googled': 0.16; 'subject:question.': 0.16; 'value:': 0.16; 'wanted.': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'solution.': 0.18; '>>>': 0.18; 'variable': 0.20; 'question.': 0.20; 'trying': 0.21; 'not,': 0.21; '>>>': 0.22; 'cc:2**0': 0.23; '>': 0.23; "haven't": 0.23; "i've": 0.23; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'question': 0.27; 'to?': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'decide': 0.28; 'url:mailman': 0.29; 'case,': 0.29; 'probably': 0.29; "i'm": 0.29; 'error': 0.30; 'lists': 0.31; 'gets': 0.32; 'url:python': 0.32; 'url:listinfo': 0.32; 'like:': 0.33; 'version': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'list': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'add': 0.36; 'url:org': 0.36; 'skip:{ 10': 0.36; 'method': 0.36; 'should': 0.36; 'enough': 0.36; 'possible': 0.37; 'does': 0.37; 'being': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'sure': 0.38; 'performance': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'url:mail': 0.40; 'most': 0.61; 'profile': 0.61; 'more': 0.63; 'results': 0.65; 'answer.': 0.71; 'obvious': 0.71; 'goal': 0.74; '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84; 'inefficient': 0.91; 'joel': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=DrZW9mo4P6/k+bGnwhU9OfhbtEtP5GndPoAcnZn+SXI=; b=xNqBxKbCvMWSXPeNDh2i2zukugwxBcH2UrX9Zi5zblC2UlDkxapzdSEEVDjTf7wcLd mmDPfBwC/up4JKiYc5OshqyAYUjHYeFUx1mUjT7dbYv3JmJgM1s/DBdCM5yQapNcdPns t2nKmHb0rU3ZeNmHY2MrjNEnzSLOceMkrOQBr2EhfKOUPf/FX8chGI/Z9pkExYnfyJxK ZEdHGAbuXKqqFnVFpzbETyvaOIXyqWwN4pSq/xRbaWiGnthj/xlVfH1n68cneCQa1nkL a8DcUbUub5dNE1lAiGkqavQQM6r2vGCegZHbgxU3vDKGcRpt59Hc2fHVtnJUVB1bSEhJ 3W4Q== MIME-Version: 1.0 In-Reply-To: <50DF4E15.1030700@lightbird.net> References: <724d4fea-606a-4503-b538-87442f6bcebc@ci3g2000vbb.googlegroups.com> <50DF4C00.2020505@lightbird.net> <50DF4E15.1030700@lightbird.net> Date: Sat, 29 Dec 2012 15:15:39 -0500 Subject: Re: dict comprehension question. From: Joel Goldstick To: Mitya Sirenef Content-Type: multipart/alternative; boundary=089e0122896c9d155204d2037168 Cc: "python-list@python.org" 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: 182 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356812146 news.xs4all.nl 6876 [2001:888:2000:d::a6]:37731 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35773 --089e0122896c9d155204d2037168 Content-Type: text/plain; charset=UTF-8 On Sat, Dec 29, 2012 at 3:09 PM, Mitya Sirenef wrote: > On 12/29/2012 03:01 PM, Mitya Sirenef wrote: > >> On 12/29/2012 02:48 PM, 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? > >> > >> TIA > > > > Will this do?: > > > > >>> w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] > > >>> {x: w.count(x) for x in w} > > {1: 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1} > > > > > > - mitya > > > > I should probably add that this might be inefficient for large lists as > it repeats count for each item. If you need it for large lists, profile > against the 'for loop' version and decide if performance is good enough > for you, for small lists it's a nice and compact solution. > > In a more general case, you can't refer to the list/dict/etc > comprehension as it's being constructed, that's just not a design goal > of comprehensions. > Would this help: >>> w = [1,2,3,1,2,4,4,5,6,1] >>> s = set(w) >>> s set([1, 2, 3, 4, 5, 6]) >>> {x:w.count(x) for x in s} {1: 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1} >>> > -m > > > -- > Lark's Tongue Guide to Python: http://lightbird.net/larks/ > > -- > http://mail.python.org/**mailman/listinfo/python-list > -- Joel Goldstick --089e0122896c9d155204d2037168 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable



On Sat, Dec 29, 2012 at 3:09 PM, Mitya Sirenef &l= t;msirenef@ligh= tbird.net> wrote:
On 12/29/2012 03:01 PM, Mitya Sirenef wrote:
On 12/29/2012 02:48 PM, Quint =C2=A0Rankid wrote:
>> Newbie question. I've googled a little and haven't found t= he answer.
>>
>> Given a list like:
>> w =3D [1, 2, 3, 1, 2, 4, 4, 5, 6, 1]
>> I would like to be able to do the following as a dict comprehensio= n.
>> a =3D {}
>> for x in w:
>> a[x] =3D 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 =3D {x:self.get(x,0)+1 for x in w}
>> results in error messages.
>>
>> And
>> a2 =3D {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<= br> >> a3 =3D {}
>> a3 =3D {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, pe= rhaps the
>> most obvious question is what instance does the get method bind to= ?
>>
>> TIA
>
> Will this do?:
>
> >>> w =3D [1, 2, 3, 1, 2, 4, 4, 5, 6, 1]
> >>> {x: w.count(x) for x in w}
> {1: 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1}
>
>
> - mitya
>

I should probably add that this might be inefficient for large lists as
it repeats count for each item. If you need it for large lists, profile
against the 'for loop' version and decide if performance is good en= ough
for you, for small lists it's a nice and compact solution.

In a more general case, you can't refer to the list/dict/etc
comprehension as it's being constructed, that's just not a design g= oal
of comprehensions.

Would this help:

=C2=A0=C2=A0=C2= =A0=C2=A0 >>> w =3D [1,2,3,1,2,4,4,5,6,1]
=C2=A0=C2=A0=C2=A0=C2= =A0 >>> s =3D set(w)
=C2=A0=C2=A0=C2=A0=C2=A0 >>> s =C2=A0=C2=A0=C2=A0=C2=A0 set([1, 2, 3, 4, 5, 6])
=C2=A0=C2=A0=C2=A0=C2= =A0 >>> {x:w.count(x) for x in s}
=C2=A0=C2=A0=C2=A0=C2=A0 {1: = 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1}
>>>


=C2=A0-m



--
Joel Goldst= ick
--089e0122896c9d155204d2037168--