Reputation: 489
I have a list that contains tuples. I need to remove any tuple that contains nan. How can I do this?
The type of nan is math.isnan. It returns True.
math.isnan(tmp[1][1][1])
I tried this and it does not work:
import math
res = [t for t in tmp if not any(isinstance(n, float) and float('nan') for n in t)]
print(res)
I also tried this and it does not work:
nans = {np.nan, math.nan, float('nan')}
[x for x in tmp if not nans.intersection(x)]
here is an example tuple
tmp = [[(0, 1.0),
(5, nan),
(10, 1.0),
(15, nan),
(20, 1.0),
(25, nan),
(30, 1.0),
(35, nan),
(40, 0.9808612440191388),
(45, nan),
(50, 0.9719298245614035),
(55, nan),
(60, 0.9712918660287081),
(65, nan),
(70, 0.8947368421052632),
(75, nan),
(80, 0.956140350877193),
(85, nan),
(90, 0.6140350877192983)],
[(0, 0.9635627530364372),
(5, nan),
(10, 1.0),
(15, nan),
(20, 0.9924812030075187),
(25, nan),
(30, 1.0),
(35, nan),
(40, 0.9808612440191388),
(45, nan),
(50, 0.9508771929824561),
(55, nan),
(60, 0.9617224880382775),
(65, nan),
(70, 0.8173374613003096),
(75, nan),
(80, 0.8464912280701754),
(85, nan),
(90, 0.5614035087719298)]]
Upvotes: 0
Views: 1346
Reputation: 36691
You can do this with a nested list comprehension. Iterate over each sublist of the list, iterate over each tuple, check if the tuple contains a nan
.
[[tup for tup in sublist if not any(map(math.isnan, tup))] for sublist in tmp]
# returns:
[[(0, 1.0),
(10, 1.0),
(20, 1.0),
(30, 1.0),
(40, 0.9808612440191388),
(50, 0.9719298245614035),
(60, 0.9712918660287081),
(70, 0.8947368421052632),
(80, 0.956140350877193),
(90, 0.6140350877192983)],
[(0, 0.9635627530364372),
(10, 1.0),
(20, 0.9924812030075187),
(30, 1.0),
(40, 0.9808612440191388),
(50, 0.9508771929824561),
(60, 0.9617224880382775),
(70, 0.8173374613003096),
(80, 0.8464912280701754),
(90, 0.5614035087719298)]]
Upvotes: 4