如何匹配两个字典中的统计信息并在新字典中按关键字分组?

2024-04-26 08:11:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个词典列表:

this_week = [
        {
          "Stat": {
            "clicks": "1822",
            "affiliate_id": "1568",
            "advertiser_id": "1892",
            "offer_id": "2423847"
          },
          "Offer": {
            "name": "app2"
          }
        },
        {
          "Stat": {
            "clicks": "11",
            "affiliate_id": "1616",
            "advertiser_id": "2171",
            "offer_id": "2402467"
          },
          "Offer": {
            "name": "two"
          }
        }
]

以及

last_week = [
        {
          "Stat": {
            "clicks": "1977",
            "affiliate_id": "1796",
            "advertiser_id": "1892",
            "offer_id": "2423847"
          },
          "Offer": {
            "name": "app2"
          }
        },
        {
          "Stat": {
            "clicks": "1248",
            "affiliate_id": "1781",
            "advertiser_id": "2171",
            "offer_id": "2402467"
          },
          "Offer": {
            "name": "two"
          }
        }
]

我想做一本像这样的词典

 items = {"1892" (advertiser_id):
            {'this_week':
                  {
                       {
                           "Stat": {
                           "clicks": "1822",
                           "affiliate_id": "1568",
                           "advertiser_id": "1892",
                           "offer_id": "2423847"
                       },
                           "Offer": {
                                "name": "app2"
                       } 
        },
    },
            {'last_week':
                  {
                       "Stat": {
                             "clicks": "1977",
                             "affiliate_id": "1796",
                             "advertiser_id": "1892",
                             "offer_id": "2423847"
                        },
                        "Offer": {
                              "name": "app2"
                        }
            },
            {'difference': 
                  { "clicks_difference": this_week['1892']['Stat']['clicks'] - last_week['1892']['Stat']['clicks'] }
         }

对于给定的广告客户标识,根据用户的选择提供标识或附属标识。这就是问题所在。两个字典中的项目顺序可能不相同,那么是否有其他方法可以按广告客户id或任何其他键对这些参数进行分组?你知道吗

如果可以更改分组id,那么如何以这种方式对这些数据进行分组?最短的方法是什么?你知道吗


Tags: nameidthis标识stat词典lastaffiliate
1条回答
网友
1楼 · 发布于 2024-04-26 08:11:44

您可以将一个列表展平为一个映射,然后在集合字段上与另一个列表循环匹配,最后计算两个列表的单击差异,例如:

def join_on(this, last, field):
    # first turn the first list into a [field_value] => [matched_stat] map:
    result = {stat["Stat"].get(field, None): {"this_week": stat} for stat in this}
    for stat in last:  # loop through the second list
        field_value = stat["Stat"].get(field, None)  # get the value of the selected field
        if field_value in result:  # check if it was already parsed in the `this` list
            result[field_value]["last_week"] = stat  # store it as last week
            # get the click value for this week:
            clicks = int(result[field_value]["this_week"]["Stat"].get("clicks", 0))
            clicks -= int(stat["Stat"].get("clicks", 0))  # subtract the last week clicks
            # store the click difference in the `difference` key
            result[field_value]["difference"] = {"clicks_difference": clicks}
        else:  # no field found in the `this` list, store it just as last week and continue..
            result[field_value]["last_week"] = stat
    return result

然后你可以用你的数据检查它:

parsed_data = join_on(this_week, last_week, "advertiser_id"))

它给出:

{'1892': {'difference': {'clicks_difference': -155},
          'last_week': {'Offer': {'name': 'app2'},
                        'Stat': {'advertiser_id': '1892',
                                 'affiliate_id': '1796',
                                 'clicks': '1977',
                                 'offer_id': '2423847'}},
          'this_week': {'Offer': {'name': 'app2'},
                        'Stat': {'advertiser_id': '1892',
                                 'affiliate_id': '1568',
                                 'clicks': '1822',
                                 'offer_id': '2423847'}}},
 '2171': {'difference': {'clicks_difference': -1237},
          'last_week': {'Offer': {'name': 'two'},
                        'Stat': {'advertiser_id': '2171',
                                 'affiliate_id': '1781',
                                 'clicks': '1248',
                                 'offer_id': '2402467'}},
          'this_week': {'Offer': {'name': 'two'},
                        'Stat': {'advertiser_id': '2171',
                                 'affiliate_id': '1616',
                                 'clicks': '11',
                                 'offer_id': '2402467'}}}}

相关问题 更多 >