以下是背景:
。
所以,我们的目标是这样说:
1: receive inbound JSON from user along with record id
2: grab the existing JSON from the database for that record id
3: for each fieldname in (a list of permitted fieldnames)
4: if the fieldname is present in the inbound JSON
5: add that field or update its contents to the existing JSON record
6: write the resulting JSON structure back to the database我的问题是,实现步骤3、4和5的最重要的Pythonic方法是什么?
我知道Python非常擅长这样的事情,而且我看到了一些非常优雅的代码,它们可以做类似的事情。
有人能建议一种非常优雅和毕达通的一般方法吗?
请注意,我只对Python 3感兴趣。
谢谢
发布于 2012-06-02 08:55:29
existing = {"a": 1, "b": 2, "c": 3}
inbound = {"b": 3, "c": 4, "d": 5}
permitted = {"a","b","c"}
existing.update((key, val) for (key, val) in inbound.items() if key in permitted)https://stackoverflow.com/questions/10860685
复制相似问题