Hi,
I have a FOR loop list consisting of 15 rows. But how can I make this FOR loop list stop at row 5?
I tried Doug Hellmann's islice() example but couldn't figure out how to integrate this into my code?
I have a FOR loop list consisting of 15 rows. But how can I make this FOR loop list stop at row 5?
# http://www.doughellmann.com/PyMOTW/itertools/index.html#module-itertools
from itertools import islice, count
j = 1
lines = open('machines.txt', 'r').readlines()
# while (j > 0):
for line in lines:
print '%2s %s' % (j, line)
j = j+1
I tried Doug Hellmann's islice() example but couldn't figure out how to integrate this into my code?
from itertools import *
print 'Stop at 5:'
for i in islice(count(), 5):
print i