Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-23 Thread Peter Otten
Pete O'Connell wrote: > Hi, I have tried to simplify things and am running into a bit of trouble. > What i am really trying to do is: Keep all the lines starting with "v " > and then delete those lines whose modulus 5 don't equal zero > > I have written it like this which seems to take a really l

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-23 Thread Dave Angel
(you replied off-list, so I'm cc'ing the list here, to keep it public) On 08/23/2012 10:42 PM, Pete O'Connell wrote: > On Fri, Aug 24, 2012 at 1:39 PM, Dave Angel wrote: > >> On 08/23/2012 09:11 PM, Pete O'Connell wrote: >>> Hi, I have tried to simplify things and am running into a bit of troubl

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-23 Thread Dave Angel
On 08/23/2012 10:34 PM, eryksun wrote: > On Thu, Aug 23, 2012 at 9:39 PM, Dave Angel wrote: >> >>> theGoodLines = [line.strip("\n") for line in lines if "v " == >>> line[0:2]] >> >> Better to use startswith(), since short lines will cause the if >> expression above to blow up. > > A slice won

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-23 Thread eryksun
On Thu, Aug 23, 2012 at 9:39 PM, Dave Angel wrote: > >> theGoodLines = [line.strip("\n") for line in lines if "v " == >> line[0:2]] > > Better to use startswith(), since short lines will cause the if > expression above to blow up. A slice won't blow up. At worst you get an empty string. But t

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-23 Thread Dave Angel
On 08/23/2012 09:11 PM, Pete O'Connell wrote: > Hi, I have tried to simplify things and am running into a bit of trouble. > What i am really trying to do is: Keep all the lines starting with "v " and > then delete those lines whose modulus 5 don't equal zero > > I have written it like this which se

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-23 Thread Pete O'Connell
Hi, I have tried to simplify things and am running into a bit of trouble. What i am really trying to do is: Keep all the lines starting with "v " and then delete those lines whose modulus 5 don't equal zero I have written it like this which seems to take a really long time (a couple of minutes wh

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-23 Thread Asokan Pichai
On Thu, Aug 23, 2012 at 2:57 AM, Pete O'Connell wrote: > On Thu, Aug 23, 2012 at 4:16 AM, Alan Gauld wrote: > >> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped >> if "vn" not in x >>

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
Ok thanks for the advice everyone. Cheers Pete On Thu, Aug 23, 2012 at 10:58 AM, Jerry Hill wrote: > On Wed, Aug 22, 2012 at 5:23 PM, Pete O'Connell > wrote: >> OK maybe I am wrong about it being slow (I thought for loops were >> slower than lis comprehensions). But I do know I need it to be a

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Jerry Hill
On Wed, Aug 22, 2012 at 5:23 PM, Pete O'Connell wrote: > OK maybe I am wrong about it being slow (I thought for loops were > slower than lis comprehensions). But I do know I need it to be as fast > as possible if I need to run it on a thousand files each with hundreds > of thousands of lines You'

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Alan Gauld
On 22/08/12 22:23, Pete O'Connell wrote: What makes you say it is "terribly slow"? Perhaps it is as fast as it could be under the circumstances. (Maybe it takes a long time because you have a lot of data, not because it is slow.) OK maybe I am wrong about it being slow (I thought for loops wer

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread ALAN GAULD
rg >Sent: Wednesday, 22 August 2012, 22:27 >Subject: Re: [Tutor] list comprehension, testing for multiple conditions > >On Thu, Aug 23, 2012 at 4:16 AM, Alan Gauld wrote: > >> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theT

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Mark Lawrence
On 22/08/2012 22:23, Pete O'Connell wrote: On Thu, Aug 23, 2012 at 2:53 AM, Steven D'Aprano wrote: Further to Steven's sound advice take a look at this http://wiki.python.org/moin/PythonSpeed/PerformanceTips IMHO the part on profiling is particulary useful. -- Cheers. Mark Lawrence. ___

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
On Thu, Aug 23, 2012 at 4:16 AM, Alan Gauld wrote: > theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped > if "vn" not in x > if "vt" not in x >

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
On Thu, Aug 23, 2012 at 2:53 AM, Steven D'Aprano wrote: > On 22/08/12 20:28, Pete O'Connell wrote: >> >> Hi. The next step for me to parse the file as I want to is to change >> lines that look like this: >> f 21/21/21 22/22/22 24/24/23 23/23/24 >> into lines that look like this: >> f 21 22 23 24 >

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
Thanks eryksun, that is a very clear example. Cheers pete On Wed, Aug 22, 2012 at 11:16 PM, eryksun wrote: > On Wed, Aug 22, 2012 at 3:06 AM, Peter Otten <__pete...@web.de> wrote: >> >> wanted = [line.strip("\n") for line in lines >> if "vn" not in line and "vt" not in line a

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Alan Gauld
On 22/08/12 10:51, Pete O'Connell wrote: theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped if "vn" not in x and "vt" not in x and x!= ""] It works but what I don't understand about this line is why the ands are not ors Because 'or' would include x if any one of the condi

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Steven D'Aprano
On 22/08/12 20:28, Pete O'Connell wrote: Hi. The next step for me to parse the file as I want to is to change lines that look like this: f 21/21/21 22/22/22 24/24/23 23/23/24 into lines that look like this: f 21 22 23 24 In English, what is the rule you are applying here? My guess is: "Given t

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Peter Otten
Pete O'Connell wrote: [Please don't to-post. Clip all text of previous posts except the portion relevant to your question] > Hi. The next step for me to parse the file as I want to is to change > lines that look like this: > f 21/21/21 22/22/22 24/24/23 23/23/24 > into lines that look like this:

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread eryksun
On Wed, Aug 22, 2012 at 3:06 AM, Peter Otten <__pete...@web.de> wrote: > > wanted = [line.strip("\n") for line in lines > if "vn" not in line and "vt" not in line and line != "\n"] Here's an equivalent expression with the negation factored out: not ("vn" in line or "vt"

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
Hi. The next step for me to parse the file as I want to is to change lines that look like this: f 21/21/21 22/22/22 24/24/23 23/23/24 into lines that look like this: f 21 22 23 24 Below is my terribly slow loop for doing this. Any suggestions about how to make this code more efficient would be gre

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
Thanks Peter. This looks like what I need: with open(fileName) as lines: wanted = [line.strip("\n") for line in lines if "vn" not in line and "vt" not in line and line != "\n"] Cheers And in response to Allan's suggestion. I can see using a generator in a situation where the if statements we

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Pete O'Connell
What a great mailing list! Thanks for all the responses. I have a few questions, though, first in regards to Puneeth's code. He writes to use: >theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped if "vn" >not in x and "vt" not in x and x!= ""] It works but what I don't understa

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Mark Lawrence
On 22/08/2012 07:29, Johann Spies wrote: #start import re exclude = re.compile('vn|vt|^$|^#') fileName = '/tmp/x' theFileOpened = open(fileName,'r') theTextAsList = theFileOpened.readlines() theTextAsListStripped = [] for aLine in theTe

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Alan Gauld
On 22/08/12 08:06, Peter Otten wrote: You can even have multiple if clauses in one list-comp (but that is rarely used): with open(filename) as lines: wanted = [line.strip("\n") for line if "vn" not in line if "vt" not in x if line !=

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-22 Thread Peter Otten
Pete O'Connell wrote: > Hi I am trying to parse a text file and create a list of all the lines > that don't include: "vn", "vt" or are empty. I want to make this as > fast as possible because I will be parsing many files each containing > thousands of lines. I though I would give list comprehensio

Re: [Tutor] list comprehension, testing for multiple conditions

2012-08-21 Thread Johann Spies
#start import re exclude = re.compile('vn|vt|^$|^#') fileName = '/tmp/x' theFileOpened = open(fileName,'r') theTextAsList = theFileOpened.readlines() theTextAsListStripped = [] for aLine in theTextAsList: theTextAsListStripped.append

[Tutor] list comprehension, testing for multiple conditions

2012-08-21 Thread Pete O'Connell
Hi I am trying to parse a text file and create a list of all the lines that don't include: "vn", "vt" or are empty. I want to make this as fast as possible because I will be parsing many files each containing thousands of lines. I though I would give list comprehensions a try. The last 3 lines of t