See also: Heapify

Go back to post Create new comment

RSS Be notified of new comments on this post with the RSS feed for this post.


That's neat but two things should be that enumerate isn't automatically applied in Python 2.3 to 2.5 and it doesn't appear to be in 2.6 either.. what version of Python is that code for? You need to explicitly call enumerate() in the versions I tested.

for i, x in enumerate(lots_of_stuff):
    if i % 20 == 0:
        print "Some progress info"
Last edited by doug on Tue, 11th Aug 2009 14:25:46

Err, true - that was a typo.. Fixed..

list = range(1,100)
result = filter(lambda y: sys.stdout.write("progress info\n"), filter(lambda x: x % 10 == 0, list))



If you only want to access every nth item you can do this.

for i in lots_of_stuff[::3]:

If you're not calculating progress based of how many time you've called the process function, for example you have another program doing the processing and your using it's API, then the following would work.

while True:
    for i in range(20):
    print( get_progress() )

Unless I was calculating progress often, I don't think I like the alternator generator, as it isn't clear what the generator does, and how to use it, and the alternative is pretty nice looking as well, however if I found myself calculating the progress a lot, then I would likely find the alternator generator to be quite useful.

Last edited by the original poster on Thu, 1st Nov 2012 20:48:02

Go back to post

Create a new comment

Go to the top

You can use a restricted version of markdown formatting here. You can use the toolbar above the text field to make this more painless. For more information about markdown please refer to the markdown cheatsheet.

For post: A neater way to print something every x iterations in Python
Your name:
Your email (optional):
Your website (optional):

A preview of your comment:

Powered by Debian, Guinness, and excessive quantities of caffeine and sugar.