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


Groups > comp.lang.python > #35791

Re: dict comprehension question.

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.nosignal.org!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'python,': 0.02; 'rewrite': 0.07; 'subject:skip:c 10': 0.07; 'python': 0.09; '[1,': 0.09; 'dict': 0.09; 'indeed,': 0.09; 'mess': 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; '{})': 0.09; 'def': 0.10; ';-)': 0.11; 'language': 0.14; 'functools': 0.16; 'iteration.': 0.16; 'places.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'sequential': 0.16; 'subject:question.': 0.16; 'value:': 0.16; 'wrote:': 0.17; 'items.': 0.17; 'jan': 0.18; 'written': 0.20; 'import': 0.21; 'explicit': 0.22; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'order.': 0.27; 'header:X-Complaints- To:1': 0.28; 'interactions': 0.29; 'prints': 0.29; 'statements': 0.29; 'wrap': 0.29; 'included': 0.29; 'source': 0.29; "skip:' 10": 0.30; 'worked': 0.30; 'function': 0.30; 'code': 0.31; 'like:': 0.33; 'to:addr:python-list': 0.33; 'list': 0.35; 'along': 0.35; 'needed': 0.35; 'filter': 0.35; 'doing': 0.35; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'functional': 0.36; 'should': 0.36; 'turn': 0.36; 'does': 0.37; 'usual': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'map': 0.61; 'between': 0.63; 'more': 0.63; 'results': 0.65; 'counts': 0.81; 'dic': 0.84; 'filtered': 0.84; 'interaction.': 0.84; 'multiply': 0.84; 'nice,': 0.84; 'received:fios.verizon.net': 0.84; 'reductions': 0.84; 'joel': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: dict comprehension question.
Date Sat, 29 Dec 2012 18:56:57 -0500
References <724d4fea-606a-4503-b538-87442f6bcebc@ci3g2000vbb.googlegroups.com>
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 <724d4fea-606a-4503-b538-87442f6bcebc@ci3g2000vbb.googlegroups.com>
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.1454.1356825458.29569.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1356825458 news.xs4all.nl 6939 [2001:888:2000:d::a6]:54341
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:35791

Show key headers only | View raw


On 12/29/2012 2:48 PM, Quint Rankid wrote:

> 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}

Let me paraphrase this: "I have nice, clear, straightforward, 
*comprehensible* code that I want to turn into an incomprehensible mess 
with a 'comprehension." That is the ironic allure of comprehensions.

Comprehensions do not allow for interactions between the source items. 
Mitya and Joel worked around this with solutions that do redundant 
calculation and multiply the time order.

Reductions do allow for interactions. Doing everything as a reduction 
was the fad before comprehensions came along ;-)

from functools import reduce
w = [1, 2, 3, 1, 2, 4, 4, 5, 6, 1]
def update(dic, n):
     "Mutate and return dic (contrary to usual Python policy)"
     dic[n] = dic.get(n, 0) + 1
     return dic
counts = reduce(update, w, {})
print(counts == {1: 3, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1})

# prints True

The above is how to rewrite your code in a functional language that does 
not have statements and explicit iteration. In Python, I would only 
bother to wrap the body of the loop in a function if I needed the same 
body in multiple places.

Comprehensions are filtered mappings and that both filter and map can be 
written as reduction, so reduction included comprehension. It is more 
powerful because it can also do sequential interaction. Indeed, I would 
say that it should only be used when there is sequential interaction.

-- 
Terry Jan Reedy

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