|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
a={"der":{"start":1,"end":1,"price":1,"type":6,"vip_sex":"","vip_id":"","VIP_money":"","id":""},"pac":[{"products":[],"first":0}]}
b=a.update({"country": "china"})
b将成为
{"der":{"start":1,"end":1,"price":1,"type":6,"vip_sex":"","vip_id":"","VIP_money":"","id":""},"pac":[{"products":[],"first":0}],"country": "china"}
如果我要想在a的字典指定的位置插入b,该怎么做?
比如
a={"der":{"start":1,"end":1,"price":1,"type":6,想要插入在这里"vip_sex":"","vip_id":"","VIP_money":"","id":""},"pac":[{"products":[想要插入在这里],"first":0}]}
本帖最后由 isdkz 于 2022-4-5 11:06 编辑
因为字典无序,所以没法改变顺序,顺序也不会影响字典的使用,所以并无意义,
你那个是直接对最外层字典进行更新,所以无法满足你的需求,你得看你要更新哪一级
- a={"der":{"start":1,"end":1,"price":1,"type":6,"vip_sex":"","vip_id":"","VIP_money":"","id":""},"pac":[{"products":[],"first":0}]}
- a['der'].update({"country": "china"})
- a['pac'][0]['products'].append({"country": "china"})
- print(a)
复制代码
运行结果:
{'der': {'start': 1, 'end': 1, 'price': 1, 'type': 6, 'vip_sex': '', 'vip_id': '', 'VIP_money': '', 'id': '', 'country': 'china'}, 'pac': [{'products': [{'country': 'china'}], 'first': 0}]}
|
|