2017-07-25 87 views
2

我有一個數據庫查詢返回三組數據。它採用這種格式。結合不同長度的多個列表中的數據

year = (('Adison', '355', 4), ('windsor windham', '455', 6), ('windham', '655', 2), ('btown', '233', 5)) 
month = (('Adison', '355', 2), ('windham', '655', 1)) 
week = (('btown', '233', 8), ('Adison', '355', 9)) 

年份列表始終是最長和最多的值。我需要從月份和星期列表中的每個元素中取出最後一個值,並將它們附加到年份列表中的適當位置,基於城鎮。

如果沒有在一個月或一週對應的值,我需要追加0。理想的情況下使它看起來像這樣:

year = (('Adison', '355', 4, 2, 9), ('windsor windham', '455', 6, 0, 0), ('windham', '655', 2, 1, 0), ('btown', '233', 5, 0, 8)) 

我試圖把兩個列表中的for循環和使用if in有條件檢查值,但我認爲我忽略了一些東西,並且正在獲取索引錯誤。我試過這樣的事情:

for each in year: 
    for part in month: 
     if part in each: 
      each.append(part[-1]) 
     else: 
      each.append(0) 

我知道必須有一個更好的方法,並且實際上可以做到這一點。是否有我應該查看的工具或模塊?我曾與zip玩過,但因爲他們長度不一樣,所以我遇到了麻煩。謝謝!

編輯

我明白,我有元組上面,在我的代碼轉換所有這些修改之前列出的對象。另外我對Python的3.6

+0

你在哪個python版本上? –

+0

我正在使用3.6。 – Joe

+0

Python元組是不可變的,所以他們沒有'.append'方法。你必須做一些像'new_tuple = old_tuple +('more','data')' –

回答

0

首先,一鍵關字典前兩個元素,採用零爲默認月份和星期。然後填寫月份和星期,如有必要:

data = {(name, n): [y, 0, 0] for name, n, y in year} 

for name, n, m in month: 
    data[name, n][1] = m 

for name, n, w in week: 
    data[name, n][2] = w 

data = tuple(tuple([*k, *v]) for k, v in data.items()) 
+1

工作完美Thankyou @wim – Joe

2

你可以建立從monthweek元組類型的字典,並獲取從這些值創建新元組追加新的值。使用dict.get(..., 0)允許設置默認爲0沒有按月或按週數據城市:

dct_mth = {k: v for k, _, v in month} 
dct_week = {k: v for k, _, v in week} 

year = list(year) # make container mutable 
for i, yr in enumerate(year): 
    year[i] += (dct_mth.get(yr[0], 0), dct_week.get(yr[0], 0)) 

print(year) 
# [('Adison', '355', 4, 2, 9), ('windsor windham', '455', 6, 0, 0), ('windham', '655', 2, 1, 0), ('btown', '233', 5, 0, 8)] 
+0

我不知道我在這裏弄錯了但它只是將0附加到每個不實際使用值更新的列表 – Joe

+0

@Joe確保字典鍵實際上包含與「year」中相同的字符串。 –

0

這個有用嗎?

year = [['Adison', '355', 4],['windsor windham', '455', 6], 
     ['windham', '655', 2],['btown', '233', 5]] 

month = [['Adison', 355, 2],['windham', '655', 1]] 

week = [['btown', '233', 8],['Adison', '355', 9]] 

for y in year: 
    for m in month: 
    if y[0] == m[0]: 
     y.append(m[-1]) 
    for w in week: 
    if y[0] == w[0]: 
     y.append(w[-1]) 

for each in year: 
    print(each) 

[ '笛聲', '355',4,2,9]

[ '溫莎Windham的', '455',6]

[ 'Windham的',」 655' ,2,1]

[ 'btown', '233',5,8]

0

下面第一代碼轉換的數據類型的字典,並然後組合數據從&這一個月的詞典到今年的詞典。最後,它將組合數據轉換爲元組列表。

我使用的是字典,因爲通過字典中的按鍵查找項目要比通過列表或元組逐條掃描查找匹配的速度快得多。只有少數幾個項目沒有太大區別,但如果您的真實數據有幾十個或幾百個城鎮,則速度差異會很大。

我假設城鎮名稱後面的數字是某種類型的ID代碼,這樣我們可以擁有多個城鎮,但名稱相同但ID號不同。因此,我的字典使用名稱和該號碼作爲關鍵。

year = (('Adison', '355', 4), ('windsor windham', '455', 6), ('windham', '655', 2), ('btown', '233', 5)) 
month = (('Adison', '355', 2), ('windham', '655', 1)) 
week = (('btown', '233', 8), ('Adison', '355', 9)) 

# Convert a nested tuple to a dict, using the first 2 fields 
# in the tuple as the key and the last field as the value. 
def make_dict(seq): 
    return {u[:-1]: [u[-1]] for u in seq} 

year = make_dict(year) 
month = make_dict(month) 
week = make_dict(week) 

# Add the month & week data to the year data 
for k in year: 
    year[k] += month.get(k, [0]) + week.get(k, [0]) 
print(year) 

# Convert the updated year dict to a list of tuples 
data = [] 
for k, v in year.items(): 
    data.append(k + tuple(v)) 
for row in data: 
    print(row) 

輸出

{('Adison', '355'): [4, 2, 9], ('windsor windham', '455'): [6, 0, 0], ('windham', '655'): [2, 1, 0], ('btown', '233'): [5, 0, 8]} 
('Adison', '355', 4, 2, 9) 
('windsor windham', '455', 6, 0, 0) 
('windham', '655', 2, 1, 0) 
('btown', '233', 5, 0, 8) 

如果你想data是一個元組的元組,只是做data = tuple(data)。 OTOH,字典形式可能比列表更有用。

相關問題