Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!border2.nntp.ams2.giganews.com!border4.nntp.ams.giganews.com!border2.nntp.ams.giganews.com!nntp.giganews.com!usenetcore.com!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'subject:skip:c 10': 0.07; '[1,': 0.09; 'collections': 0.09; 'indeed,': 0.09; 'cc:addr:python-list': 0.10; 'yet.': 0.13; 'dec': 0.15; 'sat,': 0.15; '-tkc': 0.16; 'distinct': 0.16; 'element.': 0.16; 'it..': 0.16; 'iterating': 0.16; 'subject:question.': 0.16; 'wrote:': 0.17; 'element': 0.17; 'pointer': 0.17; 'tim': 0.18; '>>>': 0.18; 'module': 0.19; 'import': 0.21; '>>>': 0.22; 'cc:2**0': 0.23; "haven't": 0.23; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'chase': 0.29; 'gather': 0.29; 'url:mailman': 0.29; 'skip:& 10': 0.29; 'url:python': 0.32; 'url:listinfo': 0.32; 'received:74.125.82': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'pm,': 0.35; 'too.': 0.35; 'except': 0.36; 'received:74.125': 0.36; 'url:org': 0.36; 'skip:{ 10': 0.36; "didn't": 0.36; 'should': 0.36; 'subject:: ': 0.38; 'header:Received:5': 0.40; 'url:mail': 0.40; 'think': 0.40; "you've": 0.61; 'skip:\xc2 10': 0.62; 'gone': 0.64; 'making': 0.64; 'results': 0.65; 'learned': 0.65; 'joel': 0.91; 'to:none': 0.93 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:cc :content-type; bh=7uZm34KLKYt0IOUlfTUbq3h4pLsh+xkckWPbZde+8lQ=; b=bczrxsezccRKjdrQWhOpyGZ74a7Otnn28e05qGasCWSjGNMZNEex8hyqCUY6MM1en/ 7Tbq1cbqA4BZ0iDrzyR5lDWtgD5kTbQElegTlDdS313FP0Kxa8SDVUcj9iz+DhxGLeQb 4Dg4cnfBrIzXIAlRi42ssMwdY0Bm38j7QkuKmaMeBrU1tUhNeE954otT4ZOPE2oqXWP7 U9qY+2CrdAInDqtu8foEnpUtehRWdjkZSfqgkZbSs1rbt2OUAIZxpERfYMgT/Sf51pMa /YIzdqmyalJ32AGGj6sFCQIcftnUk/Huu5YU8XJQh28V9AxPUEWdCnuldLK4i0ZYzPVI kpAw== MIME-Version: 1.0 In-Reply-To: <50DF8A3D.8060306@tim.thechases.com> References: <724d4fea-606a-4503-b538-87442f6bcebc@ci3g2000vbb.googlegroups.com> <50DF4C00.2020505@lightbird.net> <50DF4E15.1030700@lightbird.net> <50DF6334.7050600@lightbird.net> <50DF8A3D.8060306@tim.thechases.com> Date: Sat, 29 Dec 2012 23:10:39 -0500 Subject: Re: dict comprehension question. From: Joel Goldstick Cc: "python-list@python.org" Content-Type: multipart/alternative; boundary=bcaec53f397d545d6304d20a1481 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: 101 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356840646 news.xs4all.nl 6936 [2001:888:2000:d::a6]:36964 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35797 --bcaec53f397d545d6304d20a1481 Content-Type: text/plain; charset=UTF-8 On Sat, Dec 29, 2012 at 7:26 PM, Tim Chase wrote: > On 12/29/12 15:40, Mitya Sirenef wrote: > >> >>> 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} >>> >> >> Indeed, this is much better -- I didn't think of it.. >> > > Except that you're still overwhelmed by iterating over every element in > "w" for every distinct element. So you've gone from O(N**2) to O(k*N). > > The cleanest way to write it (IMHO) is MRAB's > > > >>> w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1] > >>> from collections import Counter > >>> results = dict(Counter(w)) > > which should gather all the statistics in one single pass across "w" > making it O(N), and it's Pythonically readable. > > -tkc > > I like this too. I haven't learned about collections module yet. Thanks for the pointer > > > -- > http://mail.python.org/**mailman/listinfo/python-list > -- Joel Goldstick --bcaec53f397d545d6304d20a1481 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable



On Sat, Dec 29, 2012 at 7:26 PM, Tim Chase <python.lis= t@tim.thechases.com> wrote:
On 12/29/12 15:40, Mitya S= irenef wrote:
=C2=A0 =C2=A0 =C2=A0 >>> w =3D [1,2,3,1,2,4,4,5,6,1]
=C2=A0 =C2=A0 =C2=A0 >>> s =3D set(w)
=C2=A0 =C2=A0 =C2=A0 >>> s
=C2=A0 =C2=A0 =C2=A0 set([1, 2, 3, 4, 5, 6])
=C2=A0 =C2=A0 =C2=A0 >>> {x:w.count(x) for x in s}
=C2=A0 =C2=A0 =C2=A0 {1: 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1}

Indeed, this is much better -- I didn't think of it..

Except that you're still overwhelmed by iterating over every element in= "w" for every distinct element. =C2=A0So you've gone from O(= N**2) to O(k*N).

The cleanest way to write it (IMHO) is MRAB's


=C2=A0>>> w =3D [1, 2, 3, 1, 2, 4, 4, 5, 6, 1]
=C2=A0>>> from collections import Counter
=C2=A0>>> results =3D dict(Counter(w))

which should gather all the statistics in one single pass across "w&qu= ot; making it O(N), and it's Pythonically readable.

-tkc

I like this too.=C2=A0 I haven't le= arned about collections module yet.=C2=A0 Thanks for the pointer
=


--
http://mail.python.org/mailman/listinfo/python-list



--
Joel Gold= stick
--bcaec53f397d545d6304d20a1481--