admin管理员组文章数量:1291309
I am writing a method that can update any item in a nested dictionary. The method takes the path, json, and value:
from functools import reduce
import operator
json_test = {'request_version': 'v1.0',
'fields': [{'team': [{'name': 'Real Madrid', 'colour': 'white'}],
'location': {'type': 'Feature',
'geometry': {'type': 'Point', 'coordinates': [0, 53]}},
'query': {'filter': '2024/25'},
'player': 'Bellingham'}]}
def update_attribute(element, json, new_value):
*path, last = element.split('.')
target = reduce(operator.getitem, path, json)
target[last] = new_value
return json
update_attribute('fields.player', json_test, 'Mbappe')
However, this method won't work as there is a list in the dictionary.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[114], line 1
----> 1 update_attribute("fields.player", json_name_ab_asr, "test")
Cell In[111], line 3, in update_attribute(element, json, new_value)
1 def update_attribute(element, json, new_value):
2 *path, last = element.split('.')
----> 3 target = reduce(operator.getitem, path, json)
4 target[last] = new_value
5 return json
TypeError: list indices must be integers or slices, not str
Any workarounds?
I am writing a method that can update any item in a nested dictionary. The method takes the path, json, and value:
from functools import reduce
import operator
json_test = {'request_version': 'v1.0',
'fields': [{'team': [{'name': 'Real Madrid', 'colour': 'white'}],
'location': {'type': 'Feature',
'geometry': {'type': 'Point', 'coordinates': [0, 53]}},
'query': {'filter': '2024/25'},
'player': 'Bellingham'}]}
def update_attribute(element, json, new_value):
*path, last = element.split('.')
target = reduce(operator.getitem, path, json)
target[last] = new_value
return json
update_attribute('fields.player', json_test, 'Mbappe')
However, this method won't work as there is a list in the dictionary.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[114], line 1
----> 1 update_attribute("fields.player", json_name_ab_asr, "test")
Cell In[111], line 3, in update_attribute(element, json, new_value)
1 def update_attribute(element, json, new_value):
2 *path, last = element.split('.')
----> 3 target = reduce(operator.getitem, path, json)
4 target[last] = new_value
5 return json
TypeError: list indices must be integers or slices, not str
Any workarounds?
Share Improve this question asked Feb 13 at 20:02 praynerprayner 4252 silver badges11 bronze badges 3 |1 Answer
Reset to default 1Convert numeric components of path
to integers. Then add the subscript to the element string when calling the function
from functools import reduce
import operator
json_test = {'request_version': 'v1.0',
'fields': [{'team': [{'name': 'Real Madrid', 'colour': 'white'}],
'location': {'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [0, 53]}},
'query': {'filter': '2024/25'},
'player': 'Bellingham'}]}
def update_attribute(element, json, new_value):
*path, last = element.split('.')
path = [int(item) if item.isdigit() else item for item in path]
target = reduce(operator.getitem, path, json)
target[last] = new_value
return json
update_attribute('fields.0.player', json_test, 'Mbappe')
This isn't perfect, as it won't work if you have a dictionary whose keys are numeric strings.
本文标签: pythonUpdate item in nested dictionary containing listStack Overflow
版权声明:本文标题:python - Update item in nested dictionary containing list - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741507637a2382417.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
fields
, being a list, doesn't have aplayer
attribute. – Scott Hunter Commented Feb 13 at 20:07