[Tutor] list comprehension, testing for multiple conditions

Pete O'Connell pedrooconnell at gmail.com
Wed Aug 22 08:05:24 CEST 2012


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 the code below have three list comprehensions that
I would like to combine into 1 but I am not sure how to do that.
Any tips would be greatly appreciated

pete

#start############################################################
fileName = '/usr/home/poconnell/Desktop/objCube.obj'
theFileOpened = open(fileName,'r')
theTextAsList = theFileOpened.readlines()

theTextAsListStripped = []
for aLine in theTextAsList:

    theTextAsListStripped.append(aLine.strip("\n"))

theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x]
theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x]
theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x != ""]
#end####################################################################

and here is the simple file I am parsing as a test:

#start##########################################################################################################
## OBJ file generated by Nuke ##

# vertex list - offset=0
v 0.000000 0.000000 1.000000
v 1.000000 0.000000 1.000000
v 0.000000 1.000000 1.000000
v 1.000000 1.000000 1.000000
v 1.000000 0.000000 0.000000
v 0.000000 0.000000 0.000000
v 1.000000 1.000000 0.000000
v 0.000000 1.000000 0.000000
v 1.000000 0.000000 1.000000
v 1.000000 0.000000 0.000000
v 1.000000 1.000000 1.000000
v 1.000000 1.000000 0.000000
v 0.000000 0.000000 0.000000
v 0.000000 0.000000 1.000000
v 0.000000 1.000000 0.000000
v 0.000000 1.000000 1.000000
v 0.000000 1.000000 1.000000
v 1.000000 1.000000 1.000000
v 0.000000 1.000000 0.000000
v 1.000000 1.000000 0.000000
v 0.000000 0.000000 0.000000
v 1.000000 0.000000 0.000000
v 0.000000 0.000000 1.000000
v 1.000000 0.000000 1.000000

# point texture coordinates - offset=0
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000

# vertex normals - offset=0
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000

f 1/1/1 2/2/2 4/4/3 3/3/4
f 5/5/5 6/6/6 8/8/7 7/7/8
f 9/9/9 10/10/10 12/12/11 11/11/12
f 13/13/13 14/14/14 16/16/15 15/15/16
f 17/17/17 18/18/18 20/20/19 19/19/20
f 21/21/21 22/22/22 24/24/23 23/23/24

# end of file


More information about the Tutor mailing list