[Tutor] Loop in pre-defined blocks
Michael Selik
michael.selik at gmail.com
Mon Jun 13 18:56:42 EDT 2016
On Mon, Jun 13, 2016 at 6:28 PM Alan Gauld via Tutor <tutor at python.org>
wrote:
> On 13/06/16 08:46, Ek Esawi wrote:
> > Here is a beginner code that might work for you. Best of luck. EK
> >
> > b=[12, 20, 35]
> >
> > for i in range(len(b)):
> > if i==0:
> > c=0
> > else:
> > c=b[i-1]
> > for j in range(c, b[i]):
> > print(i+1,j+1)
>
> The problem here is that it doesn't give the gaps in the output
> data that the OP requested. That's why we said they need the start
> and stop values in the ranges.
>
My apologies. The good news is that makes the solution even easier.
py> blocks=[(2,4), (10,13), (20,22)]
py> for i, (start, stop) in enumerate(blocks):
... for j in range(start, stop):
... print(i, j)
...
0 2
0 3
1 10
1 11
1 12
2 20
2 21
More information about the Tutor
mailing list