6 Replies - 680 Views - Last Post: 02 January 2013 - 12:06 AM Rate Topic: -----

#1 Orochimaru  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 26-December 12

how to iterate a specific number of times on a for loop list?

Posted 29 December 2012 - 08:43 AM

Hi,

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




Is This A Good Question/Topic? 0
  • +

Replies To: how to iterate a specific number of times on a for loop list?

#2 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 125
  • Joined: 08-May 11

Re: how to iterate a specific number of times on a for loop list?

Posted 29 December 2012 - 09:13 AM

View PostOrochimaru, on 29 December 2012 - 09:43 AM, said:


lines = open('machines.txt', 'r').readlines()

# while (j > 0):

for line in lines:
    print '%2s        %s' % (j, line)
    j = j+1




wouldn't this work?
for line in lines:
  if j == 5: break
  ...



Was This Post Helpful? 1
  • +
  • -

#3 Orochimaru  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 47
  • Joined: 26-December 12

Re: how to iterate a specific number of times on a for loop list?

Posted 29 December 2012 - 09:26 AM

Thanks alex that worked like a charm! :^:/>
Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3721
  • View blog
  • Posts: 12,959
  • Joined: 12-December 12

Re: how to iterate a specific number of times on a for loop list?

Posted 29 December 2012 - 09:49 AM

Just out of interest.. if you only want to print the lines then the following should work using a list comprehension (although I haven't had the chance to test it).

print ["%2s %s" % (j, line) for (j, line) in enumerate(open('machines.txt','r')) if j < 5]

Won't work in Python 3 though.
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: how to iterate a specific number of times on a for loop list?

Posted 01 January 2013 - 12:50 PM

*
POPULAR

Using an "if" statement? That's the Java way :-P

There's a very simple and clean Python way to do that - Slicing!

for line in lines[:5]: #for line in lines[0:5]: also  works
      #do stuff


This post has been edited by atraub: 01 January 2013 - 01:03 PM

Was This Post Helpful? 5
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7948
  • View blog
  • Posts: 13,535
  • Joined: 19-March 11

Re: how to iterate a specific number of times on a for loop list?

Posted 01 January 2013 - 09:51 PM

View Postatraub, on 01 January 2013 - 02:50 PM, said:

Using an "if" statement? That's the Java way :-P


To be fair, you can be a little more graceful in Java than that, but basically, yes.

The main thing you have to grok about python iterations is that they're iterations over objects, not over indexes. You can force them to act like algol loops, but that's not ever going to really work well.

So if it's important that you work with the first five items in the list, construct a list of those items. Then loop over those items, and do whatever you need to do, but when you loop over them the loop is handing you the objects directly - not an index that you can use to look up the object.

This post has been edited by jon.kiparsky: 01 January 2013 - 09:52 PM

Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: how to iterate a specific number of times on a for loop list?

Posted 02 January 2013 - 12:06 AM

To be fair, the conditional is also the C/C++/C#/PHP way AFAIK lol, I just felt like picking on Java because so many people start with that lol

This post has been edited by atraub: 02 January 2013 - 12:06 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1