2015-11-20 93 views
1

我正在尋找一個python替代連接。python合併基於鍵的字典列表

我想獲得一天的每一秒的列表,並根據時間戳將數據加入到該列表中。我有這麼遠,是這樣的:

keys=('DRIP_ID','DESCR','OBJECT','TIMESTAMP','DRIP_R1','DRIP_R2','RT_DISP1','RT_DISP2','DAY','TIME') 

的鍵是列名

rawdata=[['242418',"242418 Rechts.BD242418: tot Oudkarspel - - ${pijlop} N242 10 min - N508 14 min${pijlr}",'BD242418','20150701063825','N242','N508','10','14','20150701','063825'], 
['242418',"242418 Rechts.BD242418: tot Oudkarspel - - ${pijlop} N242 10 min - N508 14 min${pijlr}",'BD242418','20150701064327','N242','N508','10','14','20150701','064327'], 
['242418',"242418 Rechts.BD242418: tot Oudkarspel - - ${pijlop} N242 10 min - N508 14 min${pijlr}",'BD242418','20150701085717','N242','N508','10','14','20150701','085717'], 
['242418',"242418 Rechts.BD242418: tot Oudkarspel - - ${pijlop} N242 10 min - N508 14 min${pijlr}",'BD242418','20150701100116','N242','N508','10','14','20150701','100116'], 
['242418',"242418 Rechts.BD242418: tot Oudkarspel - - ${pijlop} N242 10 min - N508 14 min${pijlr}",'BD242418','20150701191611','N242','N508','10','14','20150701','191611'], 
['242418',"242418 Rechts.BD242418: tot Oudkarspel - - ${pijlop} N242 10 min - N508 14 min${pijlr}",'BD242418','20150701213616','N242','N508','10','14','20150701','213616']] 

的RAWDATA是什麼出來的軟件

sec = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51','52','53','54','55','56','57','58','59'] 
mm = sec 
hh = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23'] 
timestamp=() 
time = [] 
dictData = [] 

# Dictionary with all seconds (HHMMSS) in 1 day 
for ih, uur in enumerate(hh): 
    if ih < 24: 
      for im, minutes in enumerate(mm): 
       if im < 60: 
        for isec, secs in enumerate(sec): 
         if isec < 60: 
         timestamp = str(uur)+str(minutes)+str(secs) 
         timeDict = dict() 
         timeDict['DRIP_ID']="" 
         timeDict['DESCR']="" 
         timeDict['OBJECT']="" 
         timeDict['TIMESTAMP']="" 
         timeDict['DRIP_R1']="" 
         timeDict['DRIP_R2']="" 
         timeDict['RT_DISP1']="" 
         timeDict['RT_DISP2']="" 
         timeDict['DAY']="" 
         timeDict['TIME']=timestamp 
         time.append(timeDict) 

在這裏,我提出的所有的秒,並給他們相同的密鑰,以便於匹配

# Turn raw data into dictionary       
for row in rawdata: 
    dictionary = dict(zip(keys, row)) 
    dictData.append(dictionary) 

然後我拿RAWDATA並把它轉換成一個字典,以及

#Join, sort off 
compleet=() 
for t in time: 
    t.update(dictData) 
    compleet.append(t) 

print len(compleet) 
print compleet[1] 

然而,當我運行它,我得到的錯誤:

ValueError: dictionary update sequence element #0 has length 10; 2 is required 

這使我相信,我只能更新鍵:值對,但我不確定這是否正確。

此外:它是1:1連接。 1個時間戳只能有1個測量。 不是每一秒都有一個測量。 「加入對」將是對「TIME」

+0

我理解輸入,但不理解預期輸出。你從輸入的例子中得到了什麼輸出? –

+0

@TreyHunner我需要一張桌子,一天中的所有時間,以及他們所應用的測量。在後面的過程中,我需要填補那裏的空白。 – userBZ

+0

這聽起來像你可能需要一個字典與每個鍵的值列表:http://stackoverflow.com/a/7919673/98187 –

回答

0
#Same result as a join, by iterating. 
for iTime, t in enumerate(time): 
    for iData, d in enumerate(dictData): 
     if t['TIME'] == d['TIME']: 
      t.update(d) 

在意識到出了什麼問題後,看到沒有加入,這是最好的下一件事。

0

文檔說:

dict.update = update(...)

D.update([E, ]**F) -> None. Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]

If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v

In either case, this is followed by: for k in F: D[k] = F[k]

因爲dictData是列表,這麼想的有keys()方法,for k, v in dictData: t[k] = v是拼命地跑了update方法內並導致例外。

其實我不太瞭解你的代碼,因此我不能給這個具體的幫助。

如果您可以解釋代碼(例如執行後的正確t變量),我想幫助您。

+0

至少現在我部分理解我的錯誤。我知道我無法用一系列詞典更新字典。我想要的是用dictData列表中的適當詞典更新時間詞典。這說明了嗎? – userBZ