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


Groups > comp.lang.python > #35797

Re: dict comprehension question.

References (1 earlier) <50DF4C00.2020505@lightbird.net> <50DF4E15.1030700@lightbird.net> <CAPM-O+w8aYZ2zJcmc+ZyHOHmA7yt8OYPYAqhGWSB5tQgbnqRdA@mail.gmail.com> <50DF6334.7050600@lightbird.net> <50DF8A3D.8060306@tim.thechases.com>
Date 2012-12-29 23:10 -0500
Subject Re: dict comprehension question.
From Joel Goldstick <joel.goldstick@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1459.1356840646.29569.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On Sat, Dec 29, 2012 at 7:26 PM, Tim Chase <python.list@tim.thechases.com>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<http://mail.python.org/mailman/listinfo/python-list>
>



-- 
Joel Goldstick

Back to comp.lang.python | Previous | NextPrevious 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