简体   繁体   English

用列表值对字典进行分组

[英]grouping dictionary with list values

I have a dictionary where lists are values. 我有一本字典,其中的列表是值。 I would like to filter the data based on certain values within lists. 我想根据列表中的某些值过滤数据。 For example. 例如。 . .

inventory = {'A':['Toy',3], 'B':['Toy',8], 'C':['Cloth',15], 'D':['Cloth',9], 'E':['Toy',11]}

I would like to create another dictionary where it only shows the top priced item such that it will be. 我想创建另一本词典,在该词典中仅显示价格最高的商品。 . .

inventoryFiltered = {'C':['Cloth',15], 'E':['Toy',11]}

What code should I use to convert inventory into inventoryFiltered? 我应该使用什么代码将库存转换为库存过滤?

The end result should have top priced for each merchandise item type (such as 'Cloth', 'Toy', 'Electronic', 'Food', 'Shoes') 最终结果应针对每种商品类型(例如“服装”,“玩具”,“电子产品”,“食品”,“鞋子”)标出最高价

I only have these modules available for my system. 我的系统只有这些模块可用。

bisect
cmath
collections
datetime
functools
heapq
itertools
math
numpy
pandas
pytz
Queue
random
re
scipy
statsmodels
sklearn
talib
time
zipline

Further, I would like to accomplish one more step. 此外,我想再完成一个步骤。 Say I have one more data element (I am adding the item's days in inventory (how many days it was on the store or storage). 假设我还有一个数据元素(我要添加商品的库存天数(在商店或仓库中的天数)。

inventory = {'A':['Toy',3, 30], 'B':['Toy',8, 40], 
    'C':['Cloth',15, 50], 'D':['Cloth',9, 60], 'E':['Toy',11, 70]}.  

I would like it to do the exact same thing. 我希望它做完全相同的事情。 But keep the last element (days in inventory) 但是保留最后一个元素(库存天数)

inventoryFiltered = {'C':['Cloth',15, 50], 'E':['Toy',11, 70]}

You can sort on the items of the dictionary: 您可以对字典中的项目进行排序:

inventory = {
    'A': ['Toy', 3, 30],
    'B': ['Toy', 8, 80],
    'C': ['Cloth', 15, 150],
    'D': ['Cloth', 9, 90],
    'E': ['Toy', 11, 110]
}

items = sorted(inventory.items(), key=lambda item: item[1][1])

most_expensive_by_category = {item[0]: (key, item) for key, item in items}

most_expensive = dict(most_expensive_by_category.values())

Result: 结果:

{'C': ['Cloth', 15, 150], 'E': ['Toy', 11, 110]}

With items = sorted(inventory.items(), key=lambda item: item[1][1]) we sort the items of input dictionary by price. 使用items = sorted(inventory.items(), key=lambda item: item[1][1])我们按价格对输入字典中的项目进行排序。 Because of the sort order, most_expensive_by_category construction will keep only the most expensive item for a specific category. 由于排序顺序, most_expensive_by_category构造将仅保留特定类别中最昂贵的商品。

I would first invert the dictionary like so: 我首先会像这样反转字典:

inv={}
for k, li in inventory.items():
    inv.setdefault(li[0], []).append([k, li[1]])

>>> inv
{'Cloth': [['C', 15], ['D', 9]], 'Toy': [['A', 3], ['B', 8], ['E', 11]]}

Then getting the max of any category is trivial: 然后获取任何类别的最大值都是微不足道的:

>>> max(inv['Cloth'], key=lambda l: l[1])
['C', 15]
>>> max(inv['Toy'], key=lambda l: l[1])
['E', 11]
>>> {k:max(inv[k], key=lambda l: l[1]) for k,v in inv.items()}
{'Cloth': ['C', 15], 'Toy': ['E', 11]}

If you have a second element, like days of age, just use that in the max key value. 如果您还有第二个元素,例如年龄,只需在最大键值中使用它即可。

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

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