filter list of tuples by certain axis

Hazzaldo

I have a csv file with 3 columns of x,y,z coordinates, i.e. this format:

enter image description here

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: enter image description here

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?

user1717828

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.

edited at
0

Comments

0 comments
Login to comment

Related

List of indices of tuples of tuples that contain certain tuples

List of tuples with a certain pattern

Filter list of tuples

Filter a list of lists of tuples

Filter list of tuples in Python

How to filter list of tuples?

Plot a list of tuples on x axis

Pandas dataframe - filter list of tuples

Filter tuples in list of lists [Haskell]

Filter rows of DataFrame by list of tuples

Filter List of Tuples to Exclude from Another List of Tuples which Contains

In a list of tuples, find a match if a certain field is set

Merge/Fuse a list of tuples in a certain way

Aggregating list of tuples using equivalence of certain members in tuples (via python)

How to filter list of tuples with an item of a tuple?

Java - Flink -> Fastest way to filter List of Tuples

Filter the rows in a list of tuples using numpy

Dataframe column filter from a list of tuples

If after certain date - filter list

How to remove lists with certain words from a list of lists or list of tuples?

Iterating filter: How do I filter a list for multiple tuples?

What is the fastest way to filter list of tuples based on list of indices and values?

Scala Filter List[Int] Which Exists in other List of Tuples

How can I exclude certain 'columns' from a list of tuples?

Finding one or more strings of a certain kind in a list of tuples

Filtering out tuples nested in a list, removing specific values in certain indexes?

Keep certain elements in a list of tuples where condition is met

How to update a certain tuple element within a list of tuples?

filter a list to a certain size in Kotlin/Java