2017-07-14 66 views
1

我有詞典兩份名單:如何通過新詞典中的鍵匹配兩個字典和組中的統計信息?

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'] } 
     } 

給定ADVERTISER_ID,offer_id或者根據用戶的選擇affiliate_id。這是問題。這兩個字典中的項目順序可能不一樣,那麼還有其他方法可以通過advertiser_id或其他任何鍵將這些參數分組嗎?

如何分組這些數據這樣,如果我們可以改變分組標識?最簡單的方法是什麼?

+0

什麼你的意思是在''advertiser_id'(或'affiliate_id'或'offer_id')'?如果有任何字段匹配,就進行分組?如果是這樣的話,該字典的關鍵是什麼?你怎麼確定哪一個是'this_week',哪一個是'last_week'?並且可以在兩個這些分組的不斷重複(如果你可以根據需要將它們保持在一個列表),並留下...不清楚那麼多事情,問題... – zwer

+0

@zwer - 例如,用於ADVERTISER_ID。我的意思是,如果我們將advertiser_id更改爲任何其他密鑰,則解決方案應該是通用的。 – paus

+0

@zwer根據分組,這兩個詞典是API請求的結果,我們在哪裏選擇哪個屬性來切分統計數據。這意味着字段可能會從響應中消失,如果我們沒有在請求中提及advertiser_id,但僅提及了offer_id,我們將只在響應中看到offer_id。 – paus

回答

0

您可以拼合一個列表轉換成地圖,然後循環&匹配一組領域的其他列表,最後計算出的點擊區別,如果你有兩個列表,像:

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'}}}}