Using += in a loop
wjdandreta at worldnet.att.net
Thu Aug 8 22:49:51 CEST 2002
>t_list, s5_list = , 
>t_append, s_append = t_list.append, s5_list.append
>for q in breaks[break]:
> numeric, stringval = mkup(cost * q, list * q)
> t_append(numeric and numeric/q)
> s_append(',"%s"' % stringval or "-")
>t_list += (0, 0)
># and convert both lists to the desired types
>t = tuple(t_list)
>s5 = "".join(s5_list)
I tried your suggestions as well as the others posted. They were all
slightly slower than the original. I found profile.py and this code
section was the second slowest part.
I could not use your advice directly because I did not post the most
general case (both cost and list can be sequences). I ended up making
significant changes to the way I am writing the program which resulted
in an enormous speed gain.
I was stuck using range until I started looking through the Python
Cookbook and found the zip thing. I have wanted to be able to iterate
through sequences in parallel for a long time but did not know how.
All we need now is predecessor and successor functions (like Pascal
has). I find my self wanting to use the previous and/or next element
in a sequence sometime when looping through.
More information about the Python-list