I have a csv file with 3 columns of x,y,z coordinates, i.e. this format:
I used the code below, to import it and process its data:
import csv
from operator import itemgetter
csvfile = open(r'C:\Users\%username%\Desktop\Deep-lizard\x_y_z coor.csv')
inFile = csv.reader(csvfile)
# skip header
inFile.__next__()
#Read and sort the vertices coordinates (sort by x and y)
vertices = sorted( [(float(r[0]), float(r[1]), float(r[2])) for r in inFile], key = itemgetter(0,1) )
This turns vertices
into a list of tuples:
What I want to achieve is filter the list so that, if the third element in the tuple (i.e. the z coordinate) is bigger than 0, then include that entry (the tuple of 3 elements) in the list otherwise if it's 0 then don't include it. What's the best way of doing this?
You can put an if
statement at the end of a comprehension to filter.
In [1]: l = list(zip(range(10),range(0,20,2)))
...: l
...:
Out[1]:
[(0, 0),
(1, 2),
(2, 4),
(3, 6),
(4, 8),
(5, 10),
(6, 12),
(7, 14),
(8, 16),
(9, 18)]
In [2]: [x for x in l if x[1]<13]
Out[2]: [(0, 0), (1, 2), (2, 4), (3, 6), (4, 8), (5, 10), (6, 12)]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments