根据命令的字典创建内部值的列表 [英] Create a list of an inner value from a dict of dicts

查看:104
本文介绍了根据命令的字典创建内部值的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算dict s格的内部值的最大值和最小值.

I am trying to figure out the max and min values for an inner value of a dict of dicts.

dict看起来像这样:

{'ALLEN PHILLIP K': {'bonus': 4175000,
                     'exercised_stock_options': 1729541,
                     'expenses': 13868},
 'BADUM JAMES P': {'bonus': 'NaN',
                   'exercised_stock_options': 257817,
                   'expenses': 3486},
 ...
}

我想找出所有字典中的最小和最大exercised_stock_options.

I want to figure out the minimum and maximum exercised_stock_options across all dictionaries.

我尝试使用pandas来做到这一点,但找不到合适的方法来对数据进行整形.然后,我在Python中尝试了一个简单的for循环.我的for循环代码无法正常工作,我也无法弄清楚为什么(字典的dict被称为data_dict):

I tried using pandas to do this, but couldn't find a way to shape the data appropriately. Then, I tried a simple for-loop in Python. My code for the for-loop doesn't work, and I can't figure out why (the dict of dicts is called data_dict):

stock_options=[]
for person in range(len(data_dict)):
    stock_options.append(data_dict[person]['exercised_stock_options'])
print stock_options

然后我要获取列表的最大值和最小值.

Then I was going to take the max and min values of the list.

您知道为什么此代码不起作用吗?有什么其他方法可以计算出一个dict的内部值的最大值和最小值?

Any idea why this code doesn't work? Any alternative methods for figuring out the max and min of an inner value of a dict of dicts?

推荐答案

这是一种使用列表推导从每个字典中获取exercised_stock_options的方法,然后从数据中打印出最小值和最大值.忽略示例数据,您可以对其进行修改以适合您的需求.

Here's a method that uses a list comprehension to get the exercised_stock_options from each dictionary and then prints out the minimum and maximum value from the data. Ignore the sample data, and you can modify it to suit your needs.

d = {'John Smith':{'exercised_stock_options':99},
     'Roger Park':{'exercised_stock_options':50},
     'Tim Rogers':{'exercised_stock_options':10}}
data = [d[person]['exercised_stock_options'] for person in d]
print min(data), max(data)

这篇关于根据命令的字典创建内部值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆