[Tutor] Loop blocks
cs at zip.com.au
cs at zip.com.au
Sat Jun 11 03:54:01 EDT 2016
On 10Jun2016 22:41, Jignesh Sutar <jignesh.sutar at 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 <cs at zip.com.au>
More information about the Tutor
mailing list