【问题标题】:How to count the occurrence of an item from a list within a dictionary (python)如何计算字典中列表中项目的出现次数(python)
【发布时间】: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


    【解决方案1】:

    你的图表应该是什么样子的?
    将您的每周值打包到数据框中可以让您以任何形式表示:

    import pandas as pd
    
    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']}}
    
    def get_weekly_purchases(day_pur):
        output_dict = {}
        for _, day_val in day_pur.items():
            for pers, lst in day_val.items():
                output_dict[pers] = output_dict.setdefault(pers, {})
                for elt in lst:
                    output_dict[pers][elt] = output_dict[pers].get(elt, 0) + 1
    
        return pd.DataFrame(output_dict).fillna(0)
    
    print(get_weekly_purchases(daily_purchases))
    

    输出:

                    Edith  Carol  Hannah  Frank  Alice  Ingrid   Bob  Gertrude  Dave
    Apple            10.0   10.0    10.0    5.0    0.0     0.0   0.0       5.0  10.0
    Banana            5.0   15.0     5.0    0.0    0.0    10.0   5.0      10.0   0.0
    Hamburger Buns   10.0    0.0    10.0    0.0    5.0     0.0   5.0      10.0   5.0
    Carrot            5.0   15.0     0.0   10.0    0.0     0.0   5.0       5.0  10.0
    Dragon Fruit      0.0    5.0     5.0    5.0   10.0     0.0  10.0       0.0   0.0
    Eggs              0.0    5.0     5.0    0.0   15.0     0.0   5.0      15.0   0.0
    Ice Pops          0.0    0.0     5.0    5.0    5.0     5.0   0.0       5.0  10.0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-23
      • 2021-12-04
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多