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


Groups > comp.lang.python > #61207 > unrolled thread

Re: Eliminate "extra" variable

Started byEthan Furman <ethan@stoneleaf.us>
First post2013-12-06 15:49 -0800
Last post2013-12-06 15:49 -0800
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#61207 — Re: Eliminate "extra" variable

FromEthan Furman <ethan@stoneleaf.us>
Date2013-12-06 15:49 -0800
SubjectRe: Eliminate "extra" variable
Message-ID<mailman.3683.1386377508.18130.python-list@python.org>
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~

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web