I have 2 lists X and y with ten points coordinates. List X includes x coordinates for points 1 to 10 in order, and list y includes y coordinates for same 1 to 10 points in order. I also have 2 additional lists for point outliers: outlier_x and outlier_y. I want to go over my X and Y lists, compare their coordinates with outliers and create new 2 lists (X_new and y_new) which will include points without outliers.
There is my nested loops where I stacked. It records only first point and sims doesn`t move to the next point. Can you please help to fix it?
X_new = []
y_new = []
i = 0
n = 0
while i < len(X):
while n < len(outlier_x):
if (X[i] == outlier_x[n] and y[i] == outlier_y[n]):
continue
X_new.append(X[i])
y_new.append(y[i])
n =1
i =1
10 points coordinates:
x y
0 0.99844039
1 2.188544418
4 7.572173987
7 6.138441957
11 11.73792995
0 1.043313797
1 1.733181475
4 7.424136351
7 6.138441957
11 9.73792995
Outliers:
x y
4 7.572173987
7 6.138441957
11 11.73792995
4 7.424136351
7 6.138441957
CodePudding user response:
Maybe something like this
from random import randint
X = [randint(-5, 5) for xi in range(10)]
Y = [randint(-5, 7) for yi in range(10)]
outlier_X = [-3, 4]
outlier_Y = [-2, 6]
final_X = []
final_Y = []
for xi, yi in zip(X, Y):
x_is_valid = all([xi > ox and xi < ox for ox in outlier_X])
y_is_valid = all([yi > oy and yi < yi for oy in outlier_Y])
if x_is_valid and y_is_valid:
final_X.append(xi)
final_Y.append(yi)
CodePudding user response:
This should work:
points = set(zip(X, y))
outliers = set(zip(outlier_x, outlier_y))
X_new, y_new = zip(*(points - outliers))
After this, X_new
and y_new
will be tuples, just a heads up.