【发布时间】:2022-12-04 06:12:51
【问题描述】:
下面是一个名为daily_purchases 的字典,它映射了一个人每周(周一到周五)的购买情况,并包含他们购买的物品清单。
{'Monday': {'Edith': ['Apple', 'Banana', 'Hamburger Buns', 'Carrot', 'Hamburger Buns', 'Apple'], 'Carol': ['Carrot', 'Banana', 'Banana', 'Dragon Fruit', 'Apple', 'Apple', 'Carrot', 'Carrot', 'Eggs', 'Banana'], 'Hannah': ['Hamburger Buns', 'Dragon Fruit', 'Eggs', 'Apple', 'Apple', 'Hamburger Buns', 'Banana', 'Ice Pops'], 'Frank': ['Ice Pops', 'Carrot', 'Apple', 'Carrot', 'Dragon Fruit'], 'Alice': ['Eggs', 'Ice Pops', 'Eggs', 'Eggs', 'Hamburger Buns', 'Dragon Fruit', 'Dragon Fruit'], 'Ingrid': ['Banana', 'Banana', 'Ice Pops'], 'Bob': ['Eggs', 'Banana', 'Hamburger Buns', 'Dragon Fruit', 'Carrot', 'Dragon Fruit'], 'Gertrude': ['Hamburger Buns', 'Banana', 'Eggs', 'Ice Pops', 'Hamburger Buns', 'Eggs', 'Apple', 'Carrot', 'Eggs', 'Banana'], 'Dave': ['Apple', 'Ice Pops', 'Carrot', 'Carrot', 'Ice Pops', 'Hamburger Buns', 'Apple']}, 'Tuesday': {'Edith': ['Apple', 'Banana', 'Hamburger Buns', 'Carrot', 'Hamburger Buns', 'Apple'], 'Carol': ['Carrot', 'Banana', 'Banana', 'Dragon Fruit', 'Apple', 'Apple', 'Carrot', 'Carrot', 'Eggs', 'Banana'], 'Hannah': ['Hamburger Buns', 'Dragon Fruit', 'Eggs', 'Apple', 'Apple', 'Hamburger Buns', 'Banana', 'Ice Pops'], 'Frank': ['Ice Pops', 'Carrot', 'Apple', 'Carrot', 'Dragon Fruit'], 'Alice': ['Eggs', 'Ice Pops', 'Eggs', 'Eggs', 'Hamburger Buns', 'Dragon Fruit', 'Dragon Fruit'], 'Ingrid': ['Banana', 'Banana', 'Ice Pops'], 'Bob': ['Eggs', 'Banana', 'Hamburger Buns', 'Dragon Fruit', 'Carrot', 'Dragon Fruit'], 'Gertrude': ['Hamburger Buns', 'Banana', 'Eggs', 'Ice Pops', 'Hamburger Buns', 'Eggs', 'Apple', 'Carrot', 'Eggs', 'Banana'], 'Dave': ['Apple', 'Ice Pops', 'Carrot', 'Carrot', 'Ice Pops', 'Hamburger Buns', 'Apple']}, 'Wednesday': {'Edith': ['Apple', 'Banana', 'Hamburger Buns', 'Carrot', 'Hamburger Buns', 'Apple'], 'Carol': ['Carrot', 'Banana', 'Banana', 'Dragon Fruit', 'Apple', 'Apple', 'Carrot', 'Carrot', 'Eggs', 'Banana'], 'Hannah': ['Hamburger Buns', 'Dragon Fruit', 'Eggs', 'Apple', 'Apple', 'Hamburger Buns', 'Banana', 'Ice Pops'], 'Frank': ['Ice Pops', 'Carrot', 'Apple', 'Carrot', 'Dragon Fruit'], 'Alice': ['Eggs', 'Ice Pops', 'Eggs', 'Eggs', 'Hamburger Buns', 'Dragon Fruit', 'Dragon Fruit'], 'Ingrid': ['Banana', 'Banana', 'Ice Pops'], 'Bob': ['Eggs', 'Banana', 'Hamburger Buns', 'Dragon Fruit', 'Carrot', 'Dragon Fruit'], 'Gertrude': ['Hamburger Buns', 'Banana', 'Eggs', 'Ice Pops', 'Hamburger Buns', 'Eggs', 'Apple', 'Carrot', 'Eggs', 'Banana'], 'Dave': ['Apple', 'Ice Pops', 'Carrot', 'Carrot', 'Ice Pops', 'Hamburger Buns', 'Apple']}, 'Thursday': {'Edith': ['Apple', 'Banana', 'Hamburger Buns', 'Carrot', 'Hamburger Buns', 'Apple'], 'Carol': ['Carrot', 'Banana', 'Banana', 'Dragon Fruit', 'Apple', 'Apple', 'Carrot', 'Carrot', 'Eggs', 'Banana'], 'Hannah': ['Hamburger Buns', 'Dragon Fruit', 'Eggs', 'Apple', 'Apple', 'Hamburger Buns', 'Banana', 'Ice Pops'], 'Frank': ['Ice Pops', 'Carrot', 'Apple', 'Carrot', 'Dragon Fruit'], 'Alice': ['Eggs', 'Ice Pops', 'Eggs', 'Eggs', 'Hamburger Buns', 'Dragon Fruit', 'Dragon Fruit'], 'Ingrid': ['Banana', 'Banana', 'Ice Pops'], 'Bob': ['Eggs', 'Banana', 'Hamburger Buns', 'Dragon Fruit', 'Carrot', 'Dragon Fruit'], 'Gertrude': ['Hamburger Buns', 'Banana', 'Eggs', 'Ice Pops', 'Hamburger Buns', 'Eggs', 'Apple', 'Carrot', 'Eggs', 'Banana'], 'Dave': ['Apple', 'Ice Pops', 'Carrot', 'Carrot', 'Ice Pops', 'Hamburger Buns', 'Apple']}, 'Friday': {'Edith': ['Apple', 'Banana', 'Hamburger Buns', 'Carrot', 'Hamburger Buns', 'Apple'], 'Carol': ['Carrot', 'Banana', 'Banana', 'Dragon Fruit', 'Apple', 'Apple', 'Carrot', 'Carrot', 'Eggs', 'Banana'], 'Hannah': ['Hamburger Buns', 'Dragon Fruit', 'Eggs', 'Apple', 'Apple', 'Hamburger Buns', 'Banana', 'Ice Pops'], 'Frank': ['Ice Pops', 'Carrot', 'Apple', 'Carrot', 'Dragon Fruit'], 'Alice': ['Eggs', 'Ice Pops', 'Eggs', 'Eggs', 'Hamburger Buns', 'Dragon Fruit', 'Dragon Fruit'], 'Ingrid': ['Banana', 'Banana', 'Ice Pops'], 'Bob': ['Eggs', 'Banana', 'Hamburger Buns', 'Dragon Fruit', 'Carrot', 'Dragon Fruit'], 'Gertrude': ['Hamburger Buns', 'Banana', 'Eggs', 'Ice Pops', 'Hamburger Buns', 'Eggs', 'Apple', 'Carrot', 'Eggs', 'Banana'], 'Dave': ['Apple', 'Ice Pops', 'Carrot', 'Carrot', 'Ice Pops', 'Hamburger Buns', 'Apple']}}
最后,我想为每个人创建一周内每次购买的图表。例如,伊迪丝这周买了 X(总共)个苹果。到目前为止,这是我的代码:
def itemCounter(lst,i):
#INPUT: item list and items
#OUTPUT: count times someone bought a particular item
count = 0
for ele in lst:
if (ele ==i):
count = count + 1
return count
for day, day_values in daily_purchases.items():
for name, items in day_values.items():
for item in items:
item = itemCounter(i)
items = itemCounter(last)
我不确定哪种数据结构适合创建图形以及为什么计数器没有返回我想要的值
【问题讨论】:
标签: list function dictionary