Re: Questions: While And List Comprehension

2010-11-11 Thread James Mills
On Thu, Nov 11, 2010 at 6:10 PM, Peter Otten <__pete...@web.de> wrote: > (line for line in sys.stdin) > > and sys.stdin are equivalent as are > > [v for v in items] > > and list(items) > > So > > print list(islice(sys.stdin, 5)) I was being a little verbose ... But I like your simplification :) c

Re: Questions: While And List Comprehension

2010-11-11 Thread Felipe Vinturini
On Thu, Nov 11, 2010 at 6:10 AM, Peter Otten <__pete...@web.de> wrote: > James Mills wrote: > > > On Thu, Nov 11, 2010 at 11:01 AM, alex23 wrote: > >> +1 on this approach. Clear and obvious and not reliant on any library > >> modules other than sys. > >> > >> itertools, what WAS I thinking? :) >

Re: Questions: While And List Comprehension

2010-11-11 Thread Peter Otten
James Mills wrote: > On Thu, Nov 11, 2010 at 11:01 AM, alex23 wrote: >> +1 on this approach. Clear and obvious and not reliant on any library >> modules other than sys. >> >> itertools, what WAS I thinking? :) > > maybe: > > import sys > from itertools import islice > > print [v for v in islic

Re: Questions: While And List Comprehension

2010-11-10 Thread r0g
On 10/11/10 22:56, Emile van Sebille wrote: On 11/10/2010 4:36 AM Felipe Vinturini said... Hi Folks, I am quite new to python and I don't have a lot of experience with it yet. I have two simple questions: 1. Is there a way to limit the number of times a list comprehension will execute? E.g. I

Re: Questions: While And List Comprehension

2010-11-10 Thread James Mills
On Thu, Nov 11, 2010 at 3:05 PM, rantingrick wrote: > Hey all you guys could be mistaken. It looks like the OP is trying to > implement some sort of ghetto database. And he may NOT necessarily > want the FIRST five lines of the file. He said (and i quote) "the > values /between/ # and #". Where #

Re: Questions: While And List Comprehension

2010-11-10 Thread rantingrick
On Nov 10, 4:56 pm, Emile van Sebille wrote: > On 11/10/2010 4:36 AM Felipe Vinturini said... > > > Hi Folks, > > > I am quite new to python and I don't have a lot of experience with it yet. > > > I have two simple questions: > > > 1. Is there a way to limit the number of times a list comprehensio

Re: Questions: While And List Comprehension

2010-11-10 Thread James Mills
On Thu, Nov 11, 2010 at 11:01 AM, alex23 wrote: > +1 on this approach. Clear and obvious and not reliant on any library > modules other than sys. > > itertools, what WAS I thinking? :) maybe: import sys from itertools import islice print [v for v in islice((line for line in sys.stdin), 0, 5)]

Re: Questions: While And List Comprehension

2010-11-10 Thread James Mills
On Thu, Nov 11, 2010 at 1:02 PM, Steve Holden wrote: > This suggests that you are mistaken about not exhausting the source. Yeah I was mistaken. Oh well :) I was thinking of a generator-based solution and got lost in the implementation! -- -- James Mills -- -- "Problems are solved by method" --

Re: Questions: While And List Comprehension

2010-11-10 Thread James Mills
On Thu, Nov 11, 2010 at 11:38 AM, MRAB wrote: > 'list' will exhaust the input, then the slicing will return at most 5 > lines. Hmm you're right :) -- -- James Mills -- -- "Problems are solved by method" -- http://mail.python.org/mailman/listinfo/python-list

Re: Questions: While And List Comprehension

2010-11-10 Thread Ian
On 11/10/2010 4:36 AM Felipe Vinturini said... > 2.* *I would like to know another way, a more pythonic way, to write the > following: > === > import sys > > def Z(iNumber): >      sum=0 >      while (iNumber>=5): >    

Re: Questions: While And List Comprehension

2010-11-10 Thread Steve Holden
On 11/10/2010 7:29 PM, James Mills wrote: > On Thu, Nov 11, 2010 at 8:56 AM, Emile van Sebille wrote: >> Easiest would be print [ v for v in sys.stdin.readlines()[:5] ] but that >> still reads the entire sys.stdin (whatever it may be...) > > Here's a way of doing the same thing without consuming

Re: Questions: While And List Comprehension

2010-11-10 Thread Chris Rebert
On Wed, Nov 10, 2010 at 5:38 PM, MRAB wrote: > On 11/11/2010 00:29, James Mills wrote: >> On Thu, Nov 11, 2010 at 8:56 AM, Emile van Sebille  wrote: >>> Easiest would be print [ v for v in sys.stdin.readlines()[:5] ] but that >>> still reads the entire sys.stdin (whatever it may be...) >> >> Here'

Re: Questions: While And List Comprehension

2010-11-10 Thread MRAB
On 11/11/2010 00:29, James Mills wrote: On Thu, Nov 11, 2010 at 8:56 AM, Emile van Sebille wrote: Easiest would be print [ v for v in sys.stdin.readlines()[:5] ] but that still reads the entire sys.stdin (whatever it may be...) Here's a way of doing the same thing without consuming the entire

Re: Questions: While And List Comprehension

2010-11-10 Thread alex23
Steve Holden wrote: > how about print [sys.stdin.readline() for i in range(5)] > > At least that won't consume the whole file. +1 on this approach. Clear and obvious and not reliant on any library modules other than sys. itertools, what WAS I thinking? :) -- http://mail.python.org/mailman/listi

Re: Questions: While And List Comprehension

2010-11-10 Thread Steve Holden
On 11/10/2010 6:01 PM, James Mills wrote: > On Wed, Nov 10, 2010 at 10:36 PM, Felipe Vinturini > wrote: >> 1. Is there a way to limit the number of times a list comprehension will >> execute? E.g. I want to read from input only 5 values, so I would like >> something like (the values between # # ar

Re: Questions: While And List Comprehension

2010-11-10 Thread James Mills
On Thu, Nov 11, 2010 at 8:56 AM, Emile van Sebille wrote: > Easiest would be print [ v for v in sys.stdin.readlines()[:5] ] but that > still reads the entire sys.stdin (whatever it may be...) Here's a way of doing the same thing without consuming the entire stream (sys.stdin): #!/usr/bin/env pyt

Re: Questions: While And List Comprehension

2010-11-10 Thread James Mills
On Wed, Nov 10, 2010 at 10:36 PM, Felipe Vinturini wrote: > 1. Is there a way to limit the number of times a list comprehension will > execute? E.g. I want to read from input only 5 values, so I would like > something like (the values between # # are what I want): > ===

Re: Questions: While And List Comprehension

2010-11-10 Thread Emile van Sebille
On 11/10/2010 4:36 AM Felipe Vinturini said... Hi Folks, I am quite new to python and I don't have a lot of experience with it yet. I have two simple questions: 1. Is there a way to limit the number of times a list comprehension will execute? E.g. I want to read from input only 5 values, so I

Questions: While And List Comprehension

2010-11-10 Thread Felipe Vinturini
Hi Folks, I am quite new to python and I don't have a lot of experience with it yet. I have two simple questions: 1. Is there a way to limit the number of times a list comprehension will execute? E.g. I want to read from input only 5 values, so I would like something like (the values between # #