为了账号安全,请及时绑定邮箱和手机立即绑定

蟒蛇元组删除南

蟒蛇元组删除南

ABOUTYOU 2022-09-13 19:25:29
我有一个包含元组的列表。我需要删除任何包含nan的元组。我该怎么做?楠的类型是数学。它返回 True。math.isnan(tmp[1][1][1])我试过这个,它不起作用:import mathres = [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)]]
查看完整描述

1 回答

?
暮色呼如

TA贡献1853条经验 获得超9个赞

您可以使用嵌套列表理解来执行此操作。循环访问列表的每个子列表,循环访问每个元组,检查元组是否包含 .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)]]


查看完整回答
反对 回复 2022-09-13
  • 1 回答
  • 0 关注
  • 95 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信