On 10Jun2016 22:41, Jignesh Sutar <jignesh.su...@gmail.com> wrote:
Is there a better way to code the below than to specify blocks as I have.
Ideally I'd like to specify blocks simply as *blocks=(12,20,35)*

blocks=[(1,12), (13,20), (25,35)]
for i,j in enumerate(blocks):
   for x in xrange(blocks[i][0],blocks[i][1]+1):
       print i+1, x

For one thing I don't see you use "j" anywhere. Why not this:

 for i, block in enumerate(blocks):
     for x in xrange(block[0], block[1]+1):
         print i+1, x

enumerate() returns a counter _and_ the element from what it iterates over.

You can also start enumerate from 1 instead of zero; since the code above no longer used "i" as an index into "blocks" (because you get handed the block by enumerate) you could count from one to avoid computing "i+1".

Finally, your question: Ideally I'd like to specify blocks simply as:

 blocks=(12,20,35)

why not just do that? I don't see you using the leading number, and if it is meant to be the "i+1" in your code, you get that from enumerate already.

Cheers,
Cameron Simpson <c...@zip.com.au>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to