2016-04-29 214 views
1

如何從嵌套字典中得到最小值/最大值,該嵌套字典對於缺失值也具有'南'?python從嵌套字典中獲取最小值/最大值

*這是供參考,我找到了一個解決方案,我想我在這裏分享它,因爲我找不到答案在任何地方在stackoverflow。

實施例的數據集(ⅰ用安然數據集工作):

{ 'METTS MARK':{ '工資':365788, 'to_messages':807, 'deferral_payments': '的NaN', 'total_payments':1061827, 'exercised_stock_options': '男', '獎金':600000 ...

回答

2

來自udacity論壇,漂亮的解決方案

exercised_stock_options = [item["exercised_stock_options"] for k, item in 
data_dict.iteritems() if not item["exercised_stock_options"] == "NaN"] 
print "min is %s" % min(exercised_stock_options) 
print "max is %s" % max(exercised_stock_options) 
1

這是我如何解決了這個問題:

#get minimum and maximum stock options 

result = min(data_dict.values(), key=lambda v:v['exercised_stock_options'] if v['exercised_stock_options'] != 'NaN' else float('inf')) 
print result 

result = max(data_dict.values(), key=lambda v:v['exercised_stock_options'] if v['exercised_stock_options'] != 'NaN' else float('-inf')) 
print result 
相關問題