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


Groups > comp.lang.python > #61204

Re: Eliminate "extra" variable

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'string.': 0.05; 'string': 0.09; 'iterate': 0.09; 'subject:extra': 0.09; 'cc:addr:python- list': 0.11; 'def': 0.12; '-tkc': 0.16; '1):': 0.16; 'dict': 0.16; 'dict(': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'igor': 0.16; 'keys.': 0.16; 'looping': 0.16; 'merely': 0.16; 'stats': 0.16; 'subject:variable': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'variable': 0.18; 'bit': 0.19; 'written': 0.21; 'seems': 0.21; 'cc:addr:python.org': 0.22; 'rid': 0.24; 'skip': 0.24; 'string,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'source': 0.25; 'header:In-Reply-To:1': 0.27; 'said,': 0.30; "i'm": 0.30; 'code': 0.31; 'keys': 0.31; 'skip:d 20': 0.34; "i'd": 0.34; 'something': 0.35; 'convert': 0.35; 'objects': 0.35; 'one,': 0.35; 'point.': 0.35; 'data,': 0.36; 'doing': 0.36; 'next': 0.36; 'charset:us-ascii': 0.36; 'list': 0.37; 'subject:" ': 0.39; 'sure': 0.39; 'either': 0.39; 'even': 0.60; 'skip:z 20': 0.60; "you're": 0.61; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'date,': 0.68; '*and*': 0.84; 'received:50.22': 0.84; 'eliminates': 0.91
Date Fri, 6 Dec 2013 18:37:21 -0600
From Tim Chase <python.list@tim.thechases.com>
To Igor Korot <ikorot01@gmail.com>
Subject Re: Eliminate "extra" variable
In-Reply-To <CA+FnnTzDjRzU65+VWnOwxfVR9FGMDgHLLmLEDOoGE0BMCptzEw@mail.gmail.com>
References <CA+FnnTzDjRzU65+VWnOwxfVR9FGMDgHLLmLEDOoGE0BMCptzEw@mail.gmail.com>
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
Cc python-list@python.org
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.3681.1386376572.18130.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1386376572 news.xs4all.nl 2833 [2001:888:2000:d::a6]:35289
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:61204

Show key headers only | View raw


On 2013-12-06 11:37, Igor Korot wrote:
> def MyFunc(self, originalData):
>      data = {}
>      for i in xrange(0, len(originalData)):
>            dateStr, freq, source = originalData[i]
>            data[str(dateStr)]  = {source: freq}

this can be more cleanly/pythonically written as

  def my_func(self, original_data):
    for date, freq, source in original_data
      data[str(date)] = {source: freq}

or even just

    data = dict(
      (str(date), {source: freq})
      for date, freq, source in original_data
      )

You're calling it a "dateStr", which suggests that it's already a
string, so I'm not sure why you're str()'ing it.  So I'd either just
call it "date", or skip the str(date) bit if it's already a string.
That said, do you even need to convert it to a string (as
datetime.date objects can be used as keys in dictionaries)?

>     for i in xrange(0, len(dateStrs) - 1):
>           currDateStr = str(dateStrs[i])
>           nextDateStrs = str(dateStrs[i + 1])
> 
> It seems very strange that I need the dateStrs list just for the
> purpose of looping thru the dictionary keys.
> Can I get rid of the "dateStrs" variable?

Your code isn't actually using the data-dict at this point.  If you
were doing something with it, it might help to know what you want to
do.

Well, you can iterate over the original data, zipping them together:

  for (cur, _, _), (next, _, _) in zip(
      original_data[:-1],
      original_data[1:]
      ):
    do_something(cur, next)

If your purpose for the "data" dict is to merely look up stats from
the next one, the whole batch of your original code can be replaced
with:

  for (
        (cur_dt, cur_freq, cur_source),
        (next_dt, next_freq, next_source)
        ) in zip(original_data[:-1], original_data[1:]):
    # might need to do str(cur_dt) and str(next_dt) instead?
    do_things_with(cur_dt, cur_freq, cur_source,
      next_dt, next_freq, next_source)

That eliminates the dict *and* the extra variable name. :-)

-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-06 18:37 -0600

csiph-web