advice : how do you iterate with an acc ?
bonono at gmail.com
bonono at gmail.com
Fri Dec 2 20:08:02 EST 2005
vd12005 at yahoo.fr wrote:
> hello,
>
> i'm wondering how people from here handle this, as i often encounter
> something like:
>
> acc = [] # accumulator ;)
> for line in fileinput.input():
> if condition(line):
> if acc: #1
> doSomething(acc) #1
> acc = []
> else:
> acc.append(line)
> if acc: #2
> doSomething(acc) #2
>
> BTW i am particularly annoyed by #1 and #2 as it is a reptition, and i
> think it is quite error prone, how will you do it in a pythonic way ?
>
I think it is quite ok as the last "if acc:" is just an "end-of-stream"
implicit marker, whereas during the loop, you have explicit markers to
signal end/start of blocks. There is no unwanted variable introduced
and I don't see how it can be error prone.
This is one of the case I won't try to make it a one liner, because it
is already very natural :-)
More information about the Python-list
mailing list