2012-05-17 26 views
3

這是一個基本的python腳本,用於分析字符串並檢索參數及其值。Python Hashtable不會保存我傳給它的列表片段

import re 

link = "met_y=population&fdim_y=patientStatus:7&fdim_y=pregnant:1&scale_y=lin&ind_y=false&rdim=department&idim=department:9:2:4&idim=clinic:93301:91100:93401:41201:41100&ifdim=department&tstart=1190617200000&tend=1220511600000&ind=false&draft" 

print link 

filters = '' 

matches = re.findall("\&?(?P<name>\w+)=(?P<value>(\w|:)+)\&?",link) 
for match in matches: 
    name = match[0] 
    value = match[1] 
    selection = value.split(':') 

    filters = {} 
    print selection[0] 
    print selection[1:len(selection)] 
    filters[selection[0]] = selection[1:len(selection)] 

print filters 

這裏的問題是哈希表過濾器永遠不會獲取這些值。該腳本的輸出是

{'false': []} 

我在做什麼錯?

回答

4

您要重新filters內循環:需要

filters = {} 

這條線被放置之前的循環,而不是內部。

另一個潛在的問題是您的輸入包含重複鍵(fdim_yidim)。就目前而言,你的代碼只保留每個鍵的最後一個值。

+0

謝謝!它做了詭計。我應該花更多時間調試它,但一直認爲這是列表複製的一個問題(http://henry.precheur.org/python/copy_list)。 – Asimov4