newb loop problem
Dave
NunezD at gmail.com
Fri Aug 15 01:50:50 EDT 2008
On Aug 13, 8:46 am, Sion Arrowsmith <si... at chiark.greenend.org.uk>
wrote:
> Dave <Nun... at gmail.com> wrote:
> >hitNum = 0
> >stopCnt = 6 + hitNum
> >offSet = 5
>
> >for i in range(0,10,1):
>
> The step argument to range defaults to 1: it's tidier to omit it.
> Similarly, the start argument defaults to 0, so you can drop that too.
>
> for i in range(10):
>
> > for x in range(hitNum,len(inLst), 1):
> > print hitNum, stopCnt
>
> hitNum and stopCnt are constant in this loop: if you care about
> this print statement, move it into the outer loop and stop yourself
> drowning in output.
>
> > if x == stopCnt: break
>
> If you want to exit the inner loop when x == stopCnt, why not make
> that condition part of the loop construct?
>
> for x in range(hitNum, stopCnt):
>
> That said, if you ever see "for i in range(len(lst))" *immediately*
> replace it by "for i, x in enumerate(lst)", then go through to body
> to see if you really need that i, and if not use "for x in lst",
> with slicing if the range is more complex than range(len(lst)). As
> you can do here:
>
> for x in inLst[hitNum:stopCnt]:
> hitLst.append(x)
>
> And if all you're doing in a for loop is appending to one list from
> another, that's just what list.extend does:
>
> hitLst.extend(inLst[hitNum:stopCnt])
>
> > hitNum +=offSet
> > stopCnt+=offSet
>
> Finally, that i in the outer loop isn't being used anywhere. Why
> don't you just loop over hitNum? And noticing that stopCnt is
> increasing in lock-step with hitNum:
>
> offset = 5
>
> for hitNum in range(0, 10*offset, offset):
> hitLst.extend(inLst[hitNum:hitNum+6]
>
> --
> \S -- si... at chiark.greenend.org.uk --http://www.chaos.org.uk/~sion/
> "Frankly I have no feelings towards penguins one way or the other"
> -- Arthur C. Clarke
> her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Excellent, thanks for all your help guys.
More information about the Python-list
mailing list