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


Groups > comp.lang.python > #61193

Re: Eliminate "extra" variable

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed0.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.86.MISMATCH!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <joel.goldstick@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.024
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'iterate': 0.09; 'subject:extra': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'background.': 0.14; '1):': 0.16; 'igor': 0.16; 'keys.': 0.16; 'looping': 0.16; 'subject:variable': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; 'seems': 0.21; 'email addr:gmail.com&gt;': 0.22; 'cc:addr:python.org': 0.22; 'lets': 0.24; 'rid': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'source': 0.25; 'second': 0.26; 'code:': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'dec': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; 'directly,': 0.31; 'url:python': 0.33; 'fri,': 0.33; 'skip:d 20': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'list': 0.37; 'thank': 0.38; 'pm,': 0.38; 'skip:& 20': 0.39; 'subject:" ': 0.39; 'sure': 0.39; 'url:mail': 0.40; 'skip:\xc2 10': 0.60; 'you.': 0.62; '8bit%:10': 0.64; 'different': 0.65; 'to:addr:gmail.com': 0.65; '8bit%:100': 0.72; '\xc2\xa0\xc2\xa0': 0.74; 'me!': 0.84; 'joel': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=ARsXp/1Qths0ALREWKNHEqC4HrPsut/6dxU/tLpsLUY=; b=Bnkv/6LHUXEBzSIFKZ2Fq6k9sJZqBLAeus74e2NEW0S8m4MKLSpfbanycognfawAgc IiI7XEHJQBNglFsVNUbkmrZQj7SVi8gKLA18S/k+/lXnNxEMg6gOLKSLg/KcH8nhvOWK nX4LytGnP619g6Nah/aLXj2Dpo0/PcgVns4gW7I8M1PvRUItREA9U886GYa/DhDvEaDV s7YF/eSN5jEdeTOl1sbUPKCcRBiJjFBK40mmZ97BW58Ck+MsYuvmfQm/5r8kocUmU2q8 EMgy4jynAdgplIbVRiUKuKPxcDx29FjZfjXU6uqg3nKc+0bmTnsAoHTdsrwId771p5Qg KFEw==
MIME-Version 1.0
X-Received by 10.58.136.231 with SMTP id qd7mr420017veb.1.1386373117226; Fri, 06 Dec 2013 15:38:37 -0800 (PST)
In-Reply-To <CA+FnnTzDjRzU65+VWnOwxfVR9FGMDgHLLmLEDOoGE0BMCptzEw@mail.gmail.com>
References <CA+FnnTzDjRzU65+VWnOwxfVR9FGMDgHLLmLEDOoGE0BMCptzEw@mail.gmail.com>
Date Fri, 6 Dec 2013 18:38:37 -0500
Subject Re: Eliminate "extra" variable
From Joel Goldstick <joel.goldstick@gmail.com>
To Igor Korot <ikorot01@gmail.com>
Content-Type multipart/alternative; boundary=047d7b5d45d02de8d704ece62515
Cc "python-list@python.org" <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.3672.1386373120.18130.python-list@python.org> (permalink)
Lines 108
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1386373120 news.xs4all.nl 2971 [2001:888:2000:d::a6]:58857
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:61193

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Fri, Dec 6, 2013 at 2:37 PM, Igor Korot <ikorot01@gmail.com> wrote:

> Hi, ALL,
> I have following code:
>
> def MyFunc(self, originalData):
>      data = {}
>      dateStrs = []
>      for i in xrange(0, len(originalData)):
>            dateStr, freq, source = originalData[i]
>            data[str(dateStr)]  = {source: freq}
>
               # above line confuses me!


>            dateStrs.append(dateStr)
>     for i in xrange(0, len(dateStrs) - 1):
>           currDateStr = str(dateStrs[i])
>           nextDateStrs = str(dateStrs[i + 1])
>
>
Python lets you iterate over a list directly, so :

    for d in originalData:
        dateStr, freq, source = d
        data[source] = freq

Your code looks like you come from a c background.  Python idioms are
different

I'm not sure what you are trying to do in the second for loop, but I think
you are trying to iterate thru a dictionary in a certain order, and you
can't depend on the order

>
> 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?
>
> Thank you.
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com

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


Thread

Re: Eliminate "extra" variable Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-06 18:38 -0500
  Re: Eliminate "extra" variable Roy Smith <roy@panix.com> - 2013-12-06 19:16 -0500
    Re: Eliminate "extra" variable Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-06 19:32 -0500

csiph-web