我想從多個列表中建立一本字典。問題是,如果給定的列表爲空,那麼我不希望它包含在字典中。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': []}
注意,我不想用包含空值的鍵...
而且我不希望創建的副本不包含空值的鍵的列表...
有關如何執行此操作的任何建議?
對不起,我忘了提,我不想要一個字典的副本。 –
@ Boosted_d16:它是您剛剛構建的原始字典的淺表副本。它和你所能得到的一樣接近。只有字典對象本身是重建;鍵和值是*完全相同的對象*。 –
這是壞蛋! –