2017-10-16 54 views
2

列表的多個列表我有3個DB調用返回同一個名稱,代碼元組的元組和計數,像這樣:合併基於模板

year = (('Fanklin Grand Isle', '5560', 1), ('Windham', '3457', 1))

month = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1))

week = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1))

我需要將這些全部合併在一起,以便他們擁有姓名,代碼並計算每年,每月和每週的計數。

我的問題是,如果沒有記錄,我需要插入名稱和代碼和計數的0值。最終產品應該是這樣的:

result = (('Windham', '0905', 1, 1, 0), ('Windsor Windham', '7852', 0, 0, 0), ('Washington', '3292', 0, 0, 0), 
      ('Orleans Essex', '44072', 1, 1, 0), ('Addison', '2853', 0), ('Bennington', '3778', 0), 
      ('Fanklin Grand Isle', '5560', 0, 0 0), ('Caledonia', '1992', 0, 0, 0), 
      ('Rutland', '2395', 1, 0, 0), ('Chittendon', '3367', 1, 1, 0), ('Lamoille', '5229',0, 0 0)) 

我試圖嵌套循環來檢查,如果名字出現在DB調用和模板。如果如果將DB值附加到列表中,如果不附加0

i = 0 
for p in newlist: 
    try: 
     if p[0] == mlist[i][0]: 
      print("HERE: {} {}".format(p[0], mlist[i][0])) 
      p.append(mlist[i][-1]) 
      i += 1 
     else: 
      p.append(0) 
    except IndexError: 
     continue 

這是追加DB值但不是零。我相信必須有更好的方式來做到這一點,並讓它實際工作。

編輯

這是根據接收到的答案更新的代碼。對我來說,它仍然是一個0

數據替換每個year值:

year = (('Fanklin Grand Isle', '5560', 1), ('Windham', '0905', 0), ('Windsor Windham', '7852', 0), ('Washington', '3292', 0), ('Orleans Essex', '44072', 0), ('Chittendon', '18028633367', 1), ('Addison', '12853', 0), ('Bennington', '3778', 0), ('Caledonia', '11992', 0), ('Rutland', '1895', 0), ('Chittendon', '18367', 0), ('Lamoille', '1809', 0), ('Windham', '180905', 0), ('Windsor Windham', '180852', 0), ('Waston', '18022623292', 0), ('Orleans Essex', '18072', 0), ('Addison', '1853', 0), ('Bennington', '1778', 0), ('Fanklin Grand Isle', '18560', 0), ('Caledonia', '180292', 0), ('Rutland', '195', 0), ('Lamoille', '18229', 0)) 

month = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '18028633367', 1)) 

week = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '18367', 1)) 

代碼:

from collections import defaultdict 
joined_data = defaultdict([0, 0, 0].copy) 

for entry in year: 
    # we create the default entry by calling the defaultdict with a key 
    # and immediately grab the newly created list 
    count = joined_data[(entry[0],entry[1])] 
    # we swap *inplace* the value given by the DB query 
    count[0] = entry[2] 

# rinse and repeat with the rest of the data 
for entry in month: 
    count = joined_data[(entry[0], entry[1])] 
    count[1] = entry[2] 

for entry in week: 
    count = joined_data[(entry[0], entry[1])] 
    count[2] = entry[2] 

# Finally we format the data to the required format 
result = tuple(key+tuple(value) for key,value in joined_data.items()) 
print(result) 

結果:

(('Fanklin Grand Isle', '5560', 0, 1, 1), ('Windham', '0905', 0, 0, 0), ('Windsor Windham', '7852', 0, 0, 0), ('Washington', '3292', 0, 0, 0), ('Orleans Essex', '1072', 0, 0, 0), ('Chittendon', '13367', 0, 1, 1), ('Addison', '2853', 0, 0, 0), ('Bennington', '1878', 0, 0, 0), ('Caledonia', '1992', 0, 0, 0), ('Rutland', '2395', 0, 0, 0), ('Lamoille', '5229', 0, 0, 0))

+0

當然,所有這些城市獲得了'0'因爲如果一年一年-count在您的「年份」列表中爲「0」。你期望輸出什麼,爲什麼? –

+0

對不起tobias並不意味着編輯您的文章,意味着編輯我 – Joe

回答

0

這裏有一個方式使用defaultdict以避免關心缺少的條目來處理您的問題:

year = (('Fanklin Grand Isle', '5560', 1), ('Windham', '3457', 1)) 
month = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1)) 
week = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1)) 

#I'm using a defaultdict to deal with the missing entries 
from collections import defaultdict 
joined_data = defaultdict([0,0,0].copy) 

for entry in year: 
    #we create the default entry by calling the defaultdict with a key 
    #and immediately grab the newly created list 
    count = joined_data[(entry[0],entry[1])] 
    #we swap *inplace* the value given by the DB query 
    count[0] = entry[2] 

#rinse and repeat with the rest of the data 
for entry in month: 
    count = joined_data[(entry[0],entry[1])] 
    count[1] = entry[2] 

for entry in week: 
    count = joined_data[(entry[0],entry[1])] 
    count[2] = entry[2] 

#Finally we format the data to the required format 
result = tuple(key+tuple(value) for key,value in joined_data.items()) 
print(result) 

輸出:

>>>(('Chittendon', '3367', 0, 1, 1), ('Fanklin Grand Isle', '5560', 1, 1, 1), ('Windham', '3457', 1, 0, 0)) 
+0

這看起來很有希望,我測試了一半,它似乎很好。我將有機會盡快完成測試,如果它能正常工作,請將此標記爲答案。謝謝! – Joe

+0

即使在那裏應該有一個值,這仍然將結果中的每年輸入設置爲0。 – Joe

+1

@Joe這是如何「每年設置爲0」?富蘭克林和溫德姆顯然有一些1,與我的結果相同。請編輯您的問題並提供一些完整一致的輸入和輸出數據。 –

0

不知道我理解作爲您的示例輸入,您試圖實現的目標是n OT真正符合你的輸出,但我認爲你可以使用列表理解來構造的結果,檢查這些項目是否在yearsmonthsweeks名單,並增加了10分別爲:

>>> year = (('Fanklin Grand Isle', '5560', 1), ('Windham', '3457', 1)) 
>>> month = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1)) 
>>> week = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1)) 
>>> [(x[0], x[1], int(x in year), int(x in month), int(x in week)) for x in set(year + week + month)] 
[('Chittendon', '3367', 0, 1, 1), 
('Windham', '3457', 1, 0, 0), 
('Fanklin Grand Isle', '5560', 1, 1, 1)] 

如果這些罪名實際上可以從1是不同的,你應該先創建一些字典映射城市各自的年/月/週數,然後使用類似的列表理解如上:

>>> year_counts = {(name, code): count for (name, code, count) in year} 
>>> month_counts = {(name, code): count for (name, code, count) in month} 
>>> week_counts = {(name, code): count for (name, code, count) in week} 
>>> all_cities = [(name, code) for (name, code, count) in set(year + month + week)] 
>>> [(x[0], x[1], year_counts.get(x, 0), month_counts.get(x, 0), week_counts.get(x, 0)) for x in all_cities] 
[('Chittendon', '3367', 0, 1, 1), 
('Windham', '3457', 1, 0, 0), 
('Fanklin Grand Isle', '5560', 1, 1, 1)] 
+0

我正在嘗試創建一個報告,顯示每個位置的來電計數,按年份,月份和日期分列。問題是,這些是三個獨立的數據庫調用,所以我需要合併所有在一起,如果缺少一個位置插入名稱和0s – Joe

+0

@Joe這個問題很明顯,但我收集,我的第一個猜測沒有擊中現貨呢。現在怎麼樣?如果這仍然不是你想要的,請嘗試澄清這個問題。 –

+0

我需要每個結果都是唯一的,所以我可以通過電子郵件將報告發送給客戶。所以它應該有'姓名,代碼,年數,月數,星期數'格式。當我運行腳本 – Joe