Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'messages.': 0.04; 'newbie': 0.05; 'result,': 0.05; 'subject:skip:c 10': 0.07; '[1,': 0.09; 'dict': 0.09; 'python:': 0.09; 'bind': 0.16; 'googled': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'subject:question.': 0.16; 'value:': 0.16; 'wanted.': 0.16; 'wrote:': 0.17; 'instance': 0.17; '>>>': 0.18; 'variable': 0.20; 'question.': 0.20; 'trying': 0.21; 'not,': 0.21; "haven't": 0.23; "i've": 0.23; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'question': 0.27; 'to?': 0.27; "i'm": 0.29; 'error': 0.30; 'gets': 0.32; 'like:': 0.33; 'to:addr:python-list': 0.33; 'list': 0.35; 'doing': 0.35; 'pm,': 0.35; 'skip:{ 10': 0.36; 'method': 0.36; 'possible': 0.37; 'does': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'little': 0.39; 'received:192.168': 0.40; 'most': 0.61; 'results': 0.65; 'answer.': 0.71; 'obvious': 0.71 Date: Sat, 29 Dec 2012 15:01:04 -0500 From: Mitya Sirenef User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: dict comprehension question. References: <724d4fea-606a-4503-b538-87442f6bcebc@ci3g2000vbb.googlegroups.com> In-Reply-To: <724d4fea-606a-4503-b538-87442f6bcebc@ci3g2000vbb.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356811269 news.xs4all.nl 6903 [2001:888:2000:d::a6]:56441 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35771 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 -- Lark's Tongue Guide to Python: http://lightbird.net/larks/