简体   繁体   English

从 dicts 的 dict 创建一个内部值列表

[英]Create a list of an inner value from a dict of dicts

I am trying to figure out the max and min values for an inner value of a dict of dict s.我试图找出 dict 的dict的内部值的最大值和最小值。

The dict looks like this: dict看起来像这样:

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

I want to figure out the minimum and maximum exercised_stock_options across all dictionaries.我想找出所有字典中的最小和最大exercised_stock_options _股票选项。

I tried using pandas to do this, but couldn't find a way to shape the data appropriately.我尝试使用 Pandas 来执行此操作,但找不到适当调整数据的方法。 Then, I tried a simple for-loop in Python.然后,我在 Python 中尝试了一个简单的 for 循环。 My code for the for-loop doesn't work, and I can't figure out why (the dict of dicts is called data_dict ):我的 for 循环代码不起作用,我不知道为什么( data_dict的 dict 称为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.然后我要取列表的最大值和最小值。

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?任何用于计算 dicts 的内部值的最大值和最小值的替代方法?

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.这里有一个方法,它使用列表exercised_stock_options从每个字典中获取excured_stock_options,然后从数据中打印出最小值和最大值。 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)

You are using range to get an index number for your main dictionary.您正在使用 range 来获取主词典的索引号。 What you really should do is get the keys for the dictionary and not the index.您真正应该做的是获取字典的键而不是索引。 That is, person is the name of each one.也就是说,person 是每个人的名字。 Thus when person == 'ALLEN PHILLIP K' datadict[person] now gets the dictionary for that key.因此,当person == 'ALLEN PHILLIP K' PHILLIP person == 'ALLEN PHILLIP K' datadict[person] 现在获取该键的字典。

Note that the Use items() to iterate across dictionary says that it is better to use d, v = data_dict.items() rather than looping over the dictionary itself.请注意, 使用 items() 遍历字典表示最好使用d, v = data_dict.items()而不是循环字典本身。 Also note the difference between Python 2 and Python 3.还要注意 Python 2 和 Python 3 之间的区别。

people=[]
stock_options=[]
for person, stock_data in data_dict.items():
    people.append(person)
    stock_options.append(stock_data['exercised_stock_options'])
    # This lets you keep track of the people as well for future use
print stock_options
mymin = min(stock_options)
mymax = max(stock_options)
# process min and max values.

Best-practice最佳实践

Use items() to iterate across dictionary使用 items() 遍历字典

The updated code below demonstrates the Pythonic style for iterating through a dictionary.下面的更新代码演示了用于遍历字典的 Pythonic 风格。 When you define two variables in a for loop in conjunction with a call to items() on a dictionary, Python automatically assigns the first variable as the name of a key in that dictionary, and the second variable as the corresponding value for that key.当您在 for 循环中定义两个变量并调用字典上的 items() 时,Python 会自动将第一个变量指定为该字典中键的名称,将第二个变量指定为该键的对应值。

 d = {"first_name": "Alfred", "last_name":"Hitchcock"} for key,val in d.items(): print("{} = {}".format(key, val))

Difference Python 2 and Python 3 Python 2 和 Python 3 的区别

In python 2.x the above examples using items would return a list with tuples containing the copied key-value pairs of the dictionary.在 python 2.x 中,上面使用 items 的例子将返回一个包含字典的复制键值对的元组列表。 In order to not copy and with that load the whole dictionary's keys and values inside a list to the memory you should prefer the iteritems method which simply returns an iterator instead of a list.为了不复制并将列表中的整个字典的键和值加载到内存中,您应该更喜欢 iteritems 方法,它只返回一个迭代器而不是一个列表。 In Python 3.x the iteritems is removed and the items method returns view objects.在 Python 3.x 中, iteritems 被移除,items 方法返回视图对象。 The benefit of these view objects compared to the tuples containing copies is that every change made to the dictionary is reflected in the view objects.与包含副本的元组相比,这些视图对象的好处是对字典所做的每个更改都会反映在视图对象中。

You need to iterate your dictionary .values() and return the value of "exercised_stock_options".您需要迭代您的字典.values()并返回“exercised_stock_options”的值。 You can use a simple list comprehensions to retrieve those values您可以使用简单的列表推导来检索这些值

>>> values = [value['exercised_stock_options'] for value in d.values()]
>>> values
[257817, 1729541]
>>> min(values)
257817
>>> max(values)
1729541

I've released lifter a few weeks ago exactly for these kind of tasks, I think you may find it useful.几周前我发布了Lifter正是为了这些任务,我想你可能会发现它很有用。 The only problem here is that you have a mapping (a dict of dicts) instead of a regular iterable.这里唯一的问题是你有一个映射(一个字典的字典)而不是一个常规的可迭代对象。

Here is an answer using lifter:这是使用提升器的答案:

from lifter.models import Model

# We create a model representing our data
Person = Model('Person')

# We convert your data to a regular iterable
iterable = []
for name, data in your_data.items():
    data['name'] = name
    iterable.append(data)

# we load this into lifter
manager = Person.load(iterable)

# We query the data
results = manager.aggregate(
    (Person.exercised_stock_options, min),
    (Person.exercised_stock_options, max),
)

You can of course achieve the same result using list comprehensions, however, it's sometimes handy to use a dedicated library, especially if you want to filter data using complex queries before fetching your results.您当然可以使用列表推导式获得相同的结果,但是,有时使用专用库会很方便,特别是如果您想在获取结果之前使用复杂查询过滤数据时。 For example, you could get your min and max value only for people with less than 10000 expenses:例如,您只能为支出少于 10000 的人获取最小值和最大值:

# We filter the data
queryset = manager.filter(Person.expenses < 10000)

# we apply our aggregate on the filtered queryset
results = queryset.aggregate(
    (Person.exercised_stock_options, min),
    (Person.exercised_stock_options, max),
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM