Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!news2.euro.net!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'subject:skip:c 10': 0.07; 'indeed,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'distinct': 0.16; 'it..': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:question.': 0.16; 'wrote:': 0.17; 'items.': 0.17; 'jan': 0.18; '>>>': 0.18; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.28; 'turns': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'problem,': 0.35; 'pm,': 0.35; 'received:org': 0.36; 'skip:{ 10': 0.36; "didn't": 0.36; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'received:fios.verizon.net': 0.84; 'joel': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: dict comprehension question. Date: Sat, 29 Dec 2012 18:22:20 -0500 References: <724d4fea-606a-4503-b538-87442f6bcebc@ci3g2000vbb.googlegroups.com> <50DF4C00.2020505@lightbird.net> <50DF4E15.1030700@lightbird.net> <50DF6334.7050600@lightbird.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: <50DF6334.7050600@lightbird.net> 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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356823379 news.xs4all.nl 6908 [2001:888:2000:d::a6]:50771 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35790 On 12/29/2012 4:40 PM, Mitya Sirenef wrote: > On 12/29/2012 03:15 PM, Joel Goldstick wrote: >> 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} >> >>> > > > Indeed, this is much better -- I didn't think of it.. It still turns an O(n) problem into an O(k*n) problem, where k is the number of distinct items. -- Terry Jan Reedy