我从API获得响应,有时它有一个名为String tipo = "type_1";
for (Person person : personas) {
lista.add(new SimpleResultForm(tipo, person));
}
的可选键。当它没有密钥时,我希望在MarketPrices中有'Selections'
,如下所示:
'Selections': []
我正在尝试使用{'MarketPrices': [{'Selections': [], '_Id': 7308747L, '_ReturnCode': 16},
{'_Id': 1L, '_ReturnCode': 16}],
'ReturnStatus': {'_CallId': 7bc619bd-2805-4205-85ff-6fa5b48ea899,
'_Code': 0,
'_Description': Success},
'Timestamp': datetime.datetime(2016, 4, 3, 21, 14, 19, 726967, tzinfo=<suds.sax.date.FixedOffsetTimezone object at 0x7f805fffed50>)}
设置默认密钥,但我得到:
dict.setdefault('Selections', [])
如何指定默认值应位于{'MarketPrices': [{'Selections': [], '_Id': 7308747L, '_ReturnCode': 16},
{'_Id': 1L, '_ReturnCode': 16}],
'ReturnStatus': {'_CallId': 7bc619bd-2805-4205-85ff-6fa5b48ea899,
'_Code': 0,
'_Description': Success},
'Selections': [],
'Timestamp': datetime.datetime(2016, 4, 3, 21, 14, 19, 726967, tzinfo=<suds.sax.date.FixedOffsetTimezone object at 0x7f805fffed50>)}
?
答案 0 :(得分:2)
问题是您正在更新父词典中的“选择”的默认值
但您需要更新内部字典对象中的值。
# assuming that the super_dict object will always have "MarketPrices"
for sub_dict in super_dict['MarketPrices']:
sub_dict.setdefault('Selections':[])
提示:尽量不要使用变量名dict
,因为它会覆盖内置类型构造函数dict