Re: [Tutor] Loop in pre-defined blocks
On Mon, Jun 13, 2016 at 6:28 PM Alan Gauld via Tutor 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Loop in pre-defined blocks
OPS! This code now produces desired results. I suppose that this works for smaller blocks. For larger blocks, it might be cumbersome. EK b=[12, 20, 35] for i in range(len(b)): if i==0: c=0 elif i==2: c=24 else: c=b[i-1] for j in range(c, b[i]): print(i+1,j+1) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Loop in pre-defined blocks
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. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Loop in pre-defined blocks
On Mon, Jun 13, 2016 at 1:33 PM 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) > If you want to go a little further, you can use zip and enumerate. py> stops = [3, 5, 9] py> starts = [0] + stops[:-1] py> for i, (start, stop) in enumerate(zip(starts, stops)): ... for j in range(start, stop): ... print(i, j) ... 0 0 0 1 0 2 1 3 1 4 2 5 2 6 2 7 2 8 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Loop in pre-defined blocks
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) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Loop in pre-defined blocks
Sorry, to be a little bit more descriptive. I'd like to loop from 1 to 35 but within this loop there are divisions which I need to prefix that particular division number. My output would look like this: 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 3 25 3 26 3 27 3 28 3 29 3 30 3 31 3 32 3 33 3 34 3 35 On Sat, 11 Jun 2016 at 00:02 Alan Gauld via Tutor wrote: > On 10/06/16 23:43, Jignesh Sutar 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 > > > Can you explain in English what you are trying to do. > Working through your algorithm in my head is too much > like hard work. At the very least show us the output. > > Better still explain what it means - what the data > represents and how the outputs relate to the inputs. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Loop in pre-defined blocks
On 10/06/16 23:43, Jignesh Sutar 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 Can you explain in English what you are trying to do. Working through your algorithm in my head is too much like hard work. At the very least show us the output. Better still explain what it means - what the data represents and how the outputs relate to the inputs. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor