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


Groups > comp.lang.python > #35793

Re: dict comprehension question.

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
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; 'subject:skip:c 10': 0.07; '[1,': 0.09; 'collections': 0.09; 'indeed,': 0.09; 'cc:addr:python-list': 0.10; '-tkc': 0.16; 'distinct': 0.16; 'element.': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'it..': 0.16; 'iterating': 0.16; 'message-id:@tim.thechases.com': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'subject:question.': 0.16; 'wrote:': 0.17; 'element': 0.17; '>>>': 0.18; 'import': 0.21; 'cc:2**0': 0.23; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'gather': 0.29; 'except': 0.36; 'skip:{ 10': 0.36; "didn't": 0.36; 'should': 0.36; 'subject:: ': 0.38; 'think': 0.40; "you've": 0.61; 'gone': 0.64; 'making': 0.64; 'results': 0.65; 'received:50.22': 0.84
Date Sat, 29 Dec 2012 18:26:37 -0600
From Tim Chase <python.list@tim.thechases.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Thunderbird/17.0
MIME-Version 1.0
To Mitya Sirenef <msirenef@lightbird.net>
Subject Re: dict comprehension question.
References <724d4fea-606a-4503-b538-87442f6bcebc@ci3g2000vbb.googlegroups.com> <50DF4C00.2020505@lightbird.net> <50DF4E15.1030700@lightbird.net> <CAPM-O+w8aYZ2zJcmc+ZyHOHmA7yt8OYPYAqhGWSB5tQgbnqRdA@mail.gmail.com> <50DF6334.7050600@lightbird.net>
In-Reply-To <50DF6334.7050600@lightbird.net>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
Cc "python-list@python.org" <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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1456.1356827130.29569.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1356827130 news.xs4all.nl 6841 [2001:888:2000:d::a6]:33069
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:35793

Show key headers only | View raw


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


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