蟒蛇元组删除南

我有一个包含元组的列表。我需要删除任何包含nan的元组。我该怎么做?


楠的类型是数学。它返回 True。


math.isnan(tmp[1][1][1])

我试过这个,它不起作用:


import math

res = [t for t in tmp if not any(isinstance(n, float) and float('nan') for n in t)]

print(res)

我也试过这个,它不起作用:


nans = {np.nan, math.nan, float('nan')}

[x for x in tmp if not nans.intersection(x)] 

下面是一个元组示例


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)]]


ABOUTYOU
浏览 98回答 1
1回答

暮色呼如

您可以使用嵌套列表理解来执行此操作。循环访问列表的每个子列表,循环访问每个元组,检查元组是否包含 .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)]]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python