Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35776
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python@mrabarnett.plus.com> |
| 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; 'collections': 0.09; 'dict': 0.09; "'dict':": 0.16; 'bind': 0.16; 'class:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'googled': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 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; 'import': 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; 'received:192.168.1.3': 0.29; 'probably': 0.29; "i'm": 0.29; 'error': 0.30; 'gets': 0.32; 'received:84': 0.32; 'like:': 0.33; 'to:addr:python-list': 0.33; "can't": 0.34; 'list': 0.35; 'doing': 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; 'header :Reply-To:1': 0.68; 'answer.': 0.71; 'obvious': 0.71; 'reply-to:no real name:2**0': 0.72; 'dict,': 0.84; 'reply-to:addr:python.org': 0.84 |
| X-CM-Score | 0.00 |
| X-CNFS-Analysis | v=2.0 cv=HO4d4PRv c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=O2Kvzccb_dQA:10 a=G82AQ6m-76EA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=Fv3K-Bml36QA:10 a=9QdMJcCtvU9bKmIhhZgA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 |
| X-AUTH | mrabarnett:2500 |
| Date | Sat, 29 Dec 2012 20:39:09 +0000 |
| From | MRAB <python@mrabarnett.plus.com> |
| User-Agent | Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0 |
| 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 |
| Reply-To | python-list@python.org |
| 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.1441.1356813554.29569.python-list@python.org> (permalink) |
| Lines | 44 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1356813554 news.xs4all.nl 6898 [2001:888:2000:d::a6]:45292 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:35776 |
Show key headers only | View raw
On 2012-12-29 19:48, 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?
>
You can't do it with a comprehension.
The best way is probably with the 'Counter' class:
>>> w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1]
>>> from collections import Counter
>>> Counter(w)
Counter({1: 3, 2: 2, 4: 2, 3: 1, 5: 1, 6: 1})
If you want the result be a dict, then just the result to 'dict':
>>> dict(Counter(w))
{1: 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1}
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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