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


Groups > comp.lang.python > #61193

Re: Eliminate "extra" variable

References <CA+FnnTzDjRzU65+VWnOwxfVR9FGMDgHLLmLEDOoGE0BMCptzEw@mail.gmail.com>
Date 2013-12-06 18:38 -0500
Subject Re: Eliminate "extra" variable
From Joel Goldstick <joel.goldstick@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3672.1386373120.18130.python-list@python.org> (permalink)

Show all headers | 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