Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61193 > unrolled thread
| Started by | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| First post | 2013-12-06 18:38 -0500 |
| Last post | 2013-12-06 19:32 -0500 |
| Articles | 3 — 2 participants |
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.
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
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2013-12-06 18:38 -0500 |
| Subject | Re: Eliminate "extra" variable |
| Message-ID | <mailman.3672.1386373120.18130.python-list@python.org> |
[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
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-12-06 19:16 -0500 |
| Message-ID | <roy-D13A60.19161306122013@news.panix.com> |
| In reply to | #61193 |
In article <mailman.3672.1386373120.18130.python-list@python.org>, Joel Goldstick <joel.goldstick@gmail.com> wrote: > Python lets you iterate over a list directly, so : > > for d in originalData: > dateStr, freq, source = d > data[source] = freq I would make it even simpler: > for dateStr, freq, source in originalData: > data[source] = freq
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2013-12-06 19:32 -0500 |
| Message-ID | <mailman.3680.1386376351.18130.python-list@python.org> |
| In reply to | #61200 |
[Multipart message — attachments visible in raw view] — view raw
On Fri, Dec 6, 2013 at 7:16 PM, Roy Smith <roy@panix.com> wrote: > In article <mailman.3672.1386373120.18130.python-list@python.org>, > Joel Goldstick <joel.goldstick@gmail.com> wrote: > > > Python lets you iterate over a list directly, so : > > > > for d in originalData: > > dateStr, freq, source = d > > data[source] = freq > > I would make it even simpler: > > > for dateStr, freq, source in originalData: > > data[source] = freq > +1 --- I agree To the OP: Could you add a docstring to your function to explain what is supposed to happen, describe the input and output? If you do that I'm sure you could get some more complete help with your code. > -- > https://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web