亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從注釋中刪除 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 關注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號