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


Groups > comp.lang.python > #61313

Re: Eliminate "extra" variable

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; '(even': 0.05; 'context': 0.07; 'clean.': 0.09; 'subject:extra': 0.09; 'thread': 0.14; '-tkc': 0.16; 'dict': 0.16; 'dict(': 0.16; 'frequencies': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'key?': 0.16; 'subject:variable': 0.16; '{})': 0.16; 'language': 0.16; 'wrote:': 0.18; 'addition,': 0.20; 'appears': 0.22; 'code,': 0.22; 'tend': 0.24; 'source': 0.25; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'work.': 0.31; 'code': 0.31; 'go.': 0.31; 'occurs': 0.31; 'skip:d 20': 0.34; "can't": 0.35; 'but': 0.35; 'really': 0.36; "didn't": 0.36; 'charset:us-ascii': 0.36; 'to:addr:python-list': 0.38; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'even': 0.60; 'most': 0.60; 'such': 0.63; 'more': 0.64; 'reply': 0.66; 'date,': 0.68; 'incoming': 0.72; 'otten': 0.84; 'own)': 0.84; 'received:50.22': 0.84
Date Sun, 8 Dec 2013 12:58:20 -0600
From Tim Chase <python.list@tim.thechases.com>
To python-list@python.org
Subject Re: Eliminate "extra" variable
In-Reply-To <l81u8d$9kg$1@ger.gmane.org>
References <CA+FnnTzDjRzU65+VWnOwxfVR9FGMDgHLLmLEDOoGE0BMCptzEw@mail.gmail.com> <20131206183721.3e7f21c2@bigbox.christie.dr> <l81u8d$9kg$1@ger.gmane.org>
X-Mailer Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
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
X-Get-Message-Sender-Via boston.accountservergroup.com: authenticated_id: tim@thechases.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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3735.1386529032.18130.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1386529032 news.xs4all.nl 2864 [2001:888:2000:d::a6]:58499
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:61313

Show key headers only | View raw


On 2013-12-08 15:04, Peter Otten wrote:
> >     data = dict(
> >       (str(date), {source: freq})
> >       for date, freq, source in original_data
> >       )  
> 
> or even just
> 
> data = {str(date): {source: freq}
>         for date, freq, source in original_data}

I maintain enough pre-2.7 code that I tend to eschew
dict-comprehensions for the time being.  I like them as a language
addition, but can't use them yet in most of the code-bases with which
I work.  To the OP, if you don't have to support pre-2.7 code, then
this is a tidier way to go.

> But do you really need a dict with a single key? And is it even
> correct? If a date occurs twice only the last source:freq pair is
> kept. Without knowing the context the humble
> 
> data = {}
> for date, freq, source in original_data:
>     source_to_freq = data.setdefault(date, {})
>     if source in source_to_freq:
>         raise ValueError(
>             "Multiple frequencies for one source not supported")
>     source_to_freq[source] = freq
> 
> appears so much more plausible...

The OP's code didn't do any such sanity-checking so I made the
assumption that the data came in clean.  I know better than to trust
incoming data (even my own) in my own code, but...

(followup to the OP's reply elsewhere in the thread coming shortly)

-tkc





Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Eliminate "extra" variable Tim Chase <python.list@tim.thechases.com> - 2013-12-08 12:58 -0600

csiph-web