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


Groups > comp.lang.python > #61207

Re: Eliminate "extra" variable

Date 2013-12-06 15:49 -0800
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Eliminate "extra" variable
References <CA+FnnTzDjRzU65+VWnOwxfVR9FGMDgHLLmLEDOoGE0BMCptzEw@mail.gmail.com> <CAPM-O+wUHBtsKbEOsZ7COkrVVyhKF9N9FWLQUpGRF=G4pxOPAA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3683.1386377508.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 12/06/2013 03:38 PM, Joel Goldstick wrote:
> On Fri, Dec 6, 2013 at 2:37 PM, Igor Korot wrote:
>>
>>     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

You could shorten that to

        for dateStr, freq, source in originalData:

and if dateStr is already a string:

            data[dateStr] = {source: freq}

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

Agreed.


> 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

The second loop is iterating over the list dateStrs.

--
~Ethan~

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


Thread

Re: Eliminate "extra" variable Ethan Furman <ethan@stoneleaf.us> - 2013-12-06 15:49 -0800

csiph-web