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

从注释中删除 nan

从注释中删除 nan

UYOU 2021-08-24 19:22:55
如何删除记录用y=nan的list的dictionary像下面?[{'yanchor': 'bottom', 'xanchor': 'auto', 'x': u'2018-Q3', 'y': 169.80000000000001, 'text': '169.8', 'showarrow': False}, {'yanchor': 'bottom', 'xanchor': 'auto', 'x': u'2018-Q4', 'y': 53.829999999999998, 'text': '53.83', 'showarrow': False}, {'yanchor': 'bottom', 'xanchor': 'auto', 'x': u'2019-Q1', 'y': 63.420000000000002, 'text': '63.42', 'showarrow': False}, {'yanchor': 'bottom', 'xanchor': 'auto', 'x': u'2019-Q2', 'y': 42.369999999999997, 'text': '42.37', 'showarrow': False}, {'yanchor': 'bottom', 'xanchor': 'auto', 'x': u'2019-Q3', 'y': nan, 'text': 'nan', 'showarrow': False}, {'yanchor': 'bottom', 'xanchor': 'auto', 'x': u'2019-Q4', 'y': nan, 'text': 'nan', 'showarrow': False}]
查看完整描述

3 回答

?
慕仙森

TA贡献1827条经验 获得超8个赞

你的问题不是很好,但我会做一些假设并尝试提供帮助。

试试这个:


import numpy as np

nan = np.nan

dict_list = [{'yanchor': 'bottom', 'xanchor': 'auto', 'x': u'2018-Q3', 'y': 169.80000000000001, 'text': '169.8', 'showarrow': False}, {'yanchor': 'bottom', 'xanchor': 'auto', 'x': u'2018-Q4', 'y': 53.829999999999998, 'text': '53.83', 'showarrow': False}, {'yanchor': 'bottom', 'xanchor': 'auto', 'x': u'2019-Q1', 'y': 63.420000000000002, 'text': '63.42', 'showarrow': False}, {'yanchor': 'bottom', 'xanchor': 'auto', 'x': u'2019-Q2', 'y': 42.369999999999997, 'text': '42.37', 'showarrow': False}, {'yanchor': 'bottom', 'xanchor': 'auto', 'x': u'2019-Q3', 'y': nan, 'text': 'nan', 'showarrow': False}, {'yanchor': 'bottom', 'xanchor': 'auto', 'x': u'2019-Q4', 'y': nan, 'text': 'nan', 'showarrow': False}]

filtered_dict_list = [x for x in dict_list if not np.isnan(x['y'])]

这是一个简单的列表理解过滤,谷歌它......

祝你好运!


做出的假设:


nan 应该是 numpy.nan

“删除”是指“获取不包含不需要的字典的新字典列表


查看完整回答
反对 回复 2021-08-24
?
动漫人物

TA贡献1815条经验 获得超10个赞

下面的工作。

newrecord2 = [] for d in annotations2: if d['text'] != 'nan': # 前提是你在开头“import numpy.nan as nan” newrecord2.append(d) #print newrecord


查看完整回答
反对 回复 2021-08-24
?
杨__羊羊

TA贡献1943条经验 获得超7个赞

以下解决您的问题:


# let name your initial record "record"

# record = [{'yanchor': 'bottom', 'xanchor': 'auto', 'x': u'2018-Q3', 'y': 169.80000000000001, 'text': '169.8', 'showarrow': False},..]

#

# Let initialise a new record

newrecord = []

for d in record:

    if d['y'] is nan:   # provided that you "import numpy.nan as nan" at the beginning

       del d['y']

    newrecord.append(d)

# “newrecord”对应没有d[y] = nan的新记录。


查看完整回答
反对 回复 2021-08-24
  • 3 回答
  • 0 关注
  • 143 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号