2014-01-17 33 views
0

我想從多個列表中建立一本字典。問題是,如果給定的列表爲空,那麼我不希望它包含在字典中。Python創建字典,如果屬性被發現

繼承人的可能的密鑰

'required', 
    'as_banner', 
    'min', 
    'chart_layout', 
    'client_name', 
    'filter_text', 
    'sort_order', 
    'chart_type', 
    'chart_color', 
    'position', 
    'order' 

我的繼承人建設字典的方法:

propertyDict = dict() 
propertyDict["required"] = requiredtype 
propertyDict["as_banner"] = as_bannertype 
propertyDict["min"] = mintype 
propertyDict["chart_layout"] = chart_layouttype 
propertyDict["client_name"] = clientnametype 
propertyDict["filter_text"] = filter_texttype 
propertyDict["sort_order"] = sort_ordertype 
propertyDict["chart_type"] = charttype 
propertyDict["chart_color"] = chart_colortype 
propertyDict["position"] = positiontype 
propertyDict["order"] = ordertype 

繼承人我的腳本中,我追加鍵的值,如果在「場」的存在集合。

for table in mtd_tom.Tables: 
    if not is_profile_table(table, type="mtd"): 
     if table.Description == "Sport Parents (Regrouped)": 
      for i, subaxis in enumerate(table.Axes['Side'].SubAxes): 
       nField = shatter_sae(subaxis.Specification)['varName'] 
       field = mdd.Fields[nField] 

       if field.Properties.Item['required']: 
        requiredtype.append(field.Properties.Item['required']) 
       if field.Properties.Item['as_banner']: 
        as_bannertype.append(field.Properties.Item['as_banner']) 
       if field.Properties.Item['min']: 
        mintype.append(field.Properties.Item['min']) 
       if field.Properties.Item['chart_layout']: 
        chart_layouttype.append(field.Properties.Item['chart_layout']) 
       if field.Properties.Item['client_name']: 
        clientnametype.append(field.Properties.Item['client_name']) 
       if field.Properties.Item['filter_text']: 
        filter_texttype.append(field.Properties.Item['filter_text']) 
       if field.Properties.Item['sort_order']: 
        sort_ordertype.append(field.Properties.Item['sort_order']) 
       if field.Properties.Item['chart_type']:     
        charttype.append(field.Properties.Item['chart_type']) 
       if field.Properties.Item['chart_color']: 
        chart_colortype.append(field.Properties.Item['chart_color']) 
       if field.Properties.Item['position']: 
        positiontype.append(field.Properties.Item['position']) 
       if field.Properties.Item['order']: 
        ordertype.append(field.Properties.Item['order'])       
       else: 
        print "%s not found" % properties[i] 

繼承人的輸出,因爲它是現在:

{'as_banner': [], 'chart_color': ['green', 'pink', 'green', 'green', 'green', 'orange', 'yellow', 'white'], 'chart_layout': ['1', '2', '3', '4', '5', '6', '7', '8'], 'sort_order': ['asending', 'desending', 'asending', 'desending', 'asending', 'asending', 'asending', 'desending'], 'chart_type': ['bar', 'WeightedBar', 'column', 'pie', 'line', 'column', 'line', 'pie'], 'client_name': ['Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)'], 'filter_text': [], 'required': [], 'position': [], 'min': [], 'order': []} 

注意,我不想用包含空值的鍵...

而且我不希望創建的副本不包含空值的鍵的列表...

有關如何執行此操作的任何建議?

回答

4

這可能是最簡單的過濾空列表a壓腳提升構建詞典:

propertyDict = {k: v for k, v in propertyDict.iteritems() if v} 

另一種方法是建立在你的循環您的詞典,只添加到列表中,新加入的鍵值對;一個collections.defaultdict() instance將使容易得多

from collections import defaultdict 

propertyDict = defaultdict(list) 
properties = ('required', 'as_banner', 'min', 'chart_layout', 'client_name', 
       'filter_text', 'sort_order', 'chart_type', 'chart_color', 
       'position', 'order') 

for i, subaxis in enumerate(table.Axes['Side'].SubAxes): 
    nField = shatter_sae(subaxis.Specification)['varName'] 
    field = mdd.Fields[nField] 
    for prop in properties: 
     value = field.Properties.Item[prop] 
     if value: 
      propertyDict[prop].append(value) 

# optional, turn `defaultdict` back into a `dict` object. This step is really 
# not needed unless you need the `defaultdict.__missing__` behaviour to cease for 
# later code. 
# propertyDict = dict(propertyDict) 

此代碼是大大簡單的使用屬性名稱的列表,以測試以及

+0

對不起,我忘了提,我不想要一個字典的副本。 –

+1

@ Boosted_d16:它是您剛剛構建的原始字典的淺表副本。它和你所能得到的一樣接近。只有字典對象本身是重建;鍵和值是*完全相同的對象*。 –

+0

這是壞蛋! –

2

使用字典,理解過濾掉空鍵:

>>> d = {'as_banner': [], 'chart_color': ['green', 'pink', 'green', 'green', 'green', 'orange', 'yellow', 'white'], 'chart_layout': ['1', '2', '3', '4', '5', '6', '7', '8'], 'sort_order': ['asending', 'desending', 'asending', 'desending', 'asending', 'asending', 'asending', 'desending'], 'chart_type': ['bar', 'WeightedBar', 'column', 'pie', 'line', 'column', 'line', 'pie'], 'client_name': ['Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)'], 'filter_text': [], 'required': [], 'position': [], 'min': [], 'order': []} 
>>> {k:v for k, v in d.iteritems() if v} 
{'chart_layout': ['1', '2', '3', '4', '5', '6', '7', '8'], 'client_name': ['Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)'], 'chart_color': ['green', 'pink', 'green', 'green', 'green', 'orange', 'yellow', 'white'], 'sort_order': ['asending', 'desending', 'asending', 'desending', 'asending', 'asending', 'asending', 'desending'], 'chart_type': ['bar', 'WeightedBar', 'column', 'pie', 'line', 'column', 'line', 'pie']} 

對於python2.6的和早期使用:

dict(((k, v) for k, v in d.iteritems() if v)) 

要更新原有的字典本身與空鍵使用del值:

>>> for k, v in d.items(): 
    if not v: 
     del d[k] 

>>> d 
{'chart_layout': ['1', '2', '3', '4', '5', '6', '7', '8'], 'chart_color': ['green', 'pink', 'green', 'green', 'green', 'orange', 'yellow', 'white'], 'chart_type': ['bar', 'WeightedBar', 'column', 'pie', 'line', 'column', 'line', 'pie'], 'client_name': ['Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)', 'Sport Parents (Regrouped)'], 'sort_order': ['asending', 'desending', 'asending', 'desending', 'asending', 'asending', 'asending', 'desending']} 
+0

對不起,我忘了提及,我不想要一個字典的副本。 –

+0

@ Boosted_d16查看更新的解決方案。 –

+0

感謝您的時間,甚至不知道您可以編輯實際的字典本身,但我正在尋找Martjin做了什麼,在循環中創建字典。再次感謝! –

0

把你的類型的列表。遍歷字典和列表。你完成了!

types = [requiredtype, as_bannertype, ..] 
for k, v in propertyDict.items(): 
    for t in types: 
     if v: 
      t.append(v)