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: 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>': 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: References: Date: Fri, 6 Dec 2013 18:38:37 -0500 Subject: Re: Eliminate "extra" variable From: Joel Goldstick To: Igor Korot Content-Type: multipart/alternative; boundary=047d7b5d45d02de8d704ece62515 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --047d7b5d45d02de8d704ece62515 Content-Type: text/plain; charset=UTF-8 On Fri, Dec 6, 2013 at 2:37 PM, Igor Korot 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 --047d7b5d45d02de8d704ece62515 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On F= ri, Dec 6, 2013 at 2:37 PM, Igor Korot <ikorot01@gmail.com>= wrote:
Hi, ALL,
I have following code:

def MyFunc(self, originalData):
=C2=A0 =C2=A0 =C2=A0data =3D {}
=C2=A0 =C2=A0 =C2=A0dateStrs =3D []
=C2=A0 =C2=A0 =C2=A0for i in xrange(0, len(originalData)):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0dateStr, freq, source =3D original= Data[i]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0data[str(dateStr)] =C2=A0=3D {sour= ce: freq}
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0=C2=A0 # above line confuses me!
=C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0dateStrs.append(dateStr)
=C2=A0 =C2=A0 for i in xrange(0, len(dateStrs) - 1):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 currDateStr =3D str(dateStrs[i])
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 nextDateStrs =3D str(dateStrs[i + 1])

Python lets you iterate over a list di= rectly, so :

=C2=A0 =C2=A0 for d in originalData:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 dateStr, freq, source =3D d
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 data[source] =3D freq
=C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0
Your code looks= like you come from a c background.=C2=A0 Python idioms are different
<= br>
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



--
--047d7b5d45d02de8d704ece62515--