我有一個嵌套字典,我想根據結果串列推入一個新元素。我當前的代碼作業正常,但我想要一個多級字典和多個嵌套串列元素的通用解決方案。基本上,我想根據串列路徑使這部分動態 -注意: 串列中的專案是動態的
data['top_banner']['image']['global_image'] <---- path wil come from list element
部分字典:
{
"top_banner": {
"image": {
"global_image": {
"alt": " ",
"src": "path/to/file/5473cd34fadbb6a3-dine-1.jpg",
"sizes": [
{
"src": "",
"view": "100vw",
"media": "(min-width:100px)",
"intrinsicwidth": 0,
"intrinsicheight": 0
}
],
"types": [
"webp ",
"jpg"
],
"background": True,
"intrinsicWidth": 2400,
"intrinsicHeight": 2400
}
},
"title": "Dine",
"description": ""
}
}
串列路徑:
[['top_banner', 'image', 'global_image']]
當前代碼:
data['top_banner']['image']['global_image'].update({'intrinsicWidth': 200, 'intrinsicHeight': 300})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this should be from the list
uj5u.com熱心網友回復:
如果我理解正確,請使用functools.reduce嵌套路徑
from functools import reduce
import pprint
data = {
"top_banner": {
"image": {
"global_image": {
"alt": " ", "src": "path/to/file/5473cd34fadbb6a3-dine-1.jpg",
"sizes": [
{
"src": "", "view": "100vw", "media": "(min-width:100px)",
"intrinsicwidth": 0, "intrinsicheight": 0
}
],
"types": ["webp ", "jpg"],
"background": True, "intrinsicWidth": 2400, "intrinsicHeight": 2400
}
},
"title": "Dine", "description": ""
}
}
path = ['top_banner', 'image', 'global_image']
reduce(lambda y, x: y[x], path, data).update({'intrinsicWidth': 200, 'intrinsicHeight': 300})
pprint.pprint(data)
輸出
{'top_banner': {'description': '',
'image': {'global_image': {'alt': ' ',
'background': True,
'intrinsicHeight': 300,
'intrinsicWidth': 200,
'sizes': [{'intrinsicheight': 0,
'intrinsicwidth': 0,
'media': '(min-width:100px)',
'src': '',
'view': '100vw'}],
'src': 'path/to/file/5473cd34fadbb6a3-dine-1.jpg',
'types': ['webp ', 'jpg']}},
'title': 'Dine'}}
uj5u.com熱心網友回復:
import types
data = {
"top_banner": {
"image": {
"global_image": {
"alt": " ",
"src": "path/to/file/5473cd34fadbb6a3-dine-1.jpg",
"sizes": [
{
"src": "",
"view": "100vw",
"media": "(min-width:100px)",
"intrinsicwidth": 0,
"intrinsicheight": 0,
}
],
"types": ["webp ", "jpg"],
"background": True,
"intrinsicWidth": 2400,
"intrinsicHeight": 2400,
}
},
"title": "Dine",
"description": "",
}
}
paths = [["top_banner", "image", "global_image"]]
def recursive(data, path):
if len(path) == 0:
return data
return recursive(data[path[0]], path[1:])
for path in paths:
recursive(data, path).update({"intrinsicWidth": 200, "intrinsicHeight": 300})
print(data)
結果 :
{
"top_banner": {
"image": {
"global_image": {
"alt": " ",
"src": "path/to/file/5473cd34fadbb6a3-dine-1.jpg",
"sizes": [
{
"src": "",
"view": "100vw",
"media": "(min-width:100px)",
"intrinsicwidth": 0,
"intrinsicheight": 0,
}
],
"types": ["webp ", "jpg"],
"background": True,
"intrinsicWidth": 200,
"intrinsicHeight": 300,
}
},
"title": "Dine",
"description": "",
}
}
uj5u.com熱心網友回復:
您可以使用遞回函式:
def get_dict_element_by_path(dic, lst):
if lst:
return get_dict_element_by_path(dic[lst[0]], lst[1:])
else:
return dic
my_list = ['top_banner', 'image', 'global_image']
get_dict_element_by_path(data, my_list).update({'intrinsicWidth': 200, 'intrinsicHeight': 300})
uj5u.com熱心網友回復:
或者你可以這樣做 whitout recursion
data = {
"top_banner": {
"image": {
"global_image": {
"alt": " ",
"src": "path/to/file/5473cd34fadbb6a3-dine-1.jpg",
"sizes": [
{
"src": "",
"view": "100vw",
"media": "(min-width:100px)",
"intrinsicwidth": 0,
"intrinsicheight": 0,
}
],
"types": ["webp ", "jpg"],
"background": True,
"intrinsicWidth": 2400,
"intrinsicHeight": 2400,
}
},
"title": "Dine",
"description": "",
}
}
paths = [["top_banner", "image", "global_image"]]
res = data
for path in paths:
for p in path:
res = res[p]
res.update({"intrinsicWidth": 200, "intrinsicHeight": 300})
print(data)
這就是結果
{
"top_banner": {
"image": {
"global_image": {
"alt": " ",
"src": "path/to/file/5473cd34fadbb6a3-dine-1.jpg",
"sizes": [
{
"src": "",
"view": "100vw",
"media": "(min-width:100px)",
"intrinsicwidth": 0,
"intrinsicheight": 0,
}
],
"types": ["webp ", "jpg"],
"background": True,
"intrinsicWidth": 200,
"intrinsicHeight": 300,
}
},
"title": "Dine",
"description": "",
}
}
uj5u.com熱心網友回復:
您可以nestedDict基于 dict創建一個類并覆寫其 get、getitem 和 setitem 方法以支持按路徑進行索引。這將使索引和操作更加無縫:
class nestedDict(dict):
def __getDictKey(self,index):
parent,path = self,[index]
while True:
key = path.pop(0)
if isinstance(key,(list,tuple)): path[:0] = key
elif path: parent = parent[key]
else: break
return parent,key
def __getitem__(self,index):
d,k = self.__getDictKey(index)
return dict.__getitem__(d,k)
def get(self,index,default=None):
try: return self[index]
except KeyError: return default
def __setitem__(self,index,value):
d,k = self.__getDictKey(index)
dict.__setitem__(d,k,value)
用法示例:
data = nestedDict(data)
# Indexing by path:
data['top_banner', 'image','global_image','src']
'path/to/file/5473cd34fadbb6a3-dine-1.jpg'
data['top_banner', 'image','global_image','background'] = False
# Indexing by dynamic paths:
path = ['top_banner', 'image','global_image']
data[path]
{'alt':' ', 'src':'path/to/file/5473cd34fadbb6a3-dine-1.jpg', 'sizes': ... }
data[path,"types"]
['webp ', 'jpg']
data[path].update({'intrinsicwidth': 200, 'intrinsicheight': 300})
data[path,"types"].append('pdf')
data[path,'background'] = False
# Indexing by composite paths:
pathParts = [['top_banner', 'image'],['global_image','types']]
data[pathParts]
['webp ', 'jpg']
要對不同路徑的串列執行相同的更新,您應該使用回圈:
paths = [path1,path2,path3]
newSize = {'intrinsicwidth': 200, 'intrinsicheight': 300}
for path in paths:
data[path,"global_image"].update(newSize)
最后,您可能還想覆寫鍵、值和專案方法,以允許對不同長度的路徑進行迭代:
def keys(self,depth=1):
if depth ==1 :
yield from ([k] for k in dict.keys(self))
else:
for k,d in dict.items(self):
if not isinstance(d,dict): continue
yield from ([k] p for p in nestedDict.keys(d,depth-1))
def values(self,depth=1):
yield from (self[k] for k in self.keys(depth))
def items(self,depth=1):
return zip(self.keys(depth),self.values(depth)))
這將允許諸如:
# get all 2-part paths
for p in data.keys(2): print(p)
['top_banner', 'image']
['top_banner', 'title']
['top_banner', 'description']
# all level 4 paths and values
for k,v in data.items(4):print(k,":",v)
['top_banner', 'image', 'global_image', 'alt'] :
['top_banner', 'image', 'global_image', 'src'] : path/to/file/5473cd34fadbb6a3-dine-1.jpg
['top_banner', 'image', 'global_image', 'sizes'] : [{'src': '', 'view': '100vw', 'media': '(min-width:100px)', 'intrinsicwidth': 0, 'intrinsicheight': 0}]
['top_banner', 'image', 'global_image', 'types'] : ['webp ', 'jpg']
['top_banner', 'image', 'global_image', 'background'] : True
['top_banner', 'image', 'global_image', 'intrinsicWidth'] : 2400
['top_banner', 'image', 'global_image', 'intrinsicHeight'] : 2400
# All 'types' values (at level 3)
for v in data.values(3):print(v['types'])
['webp ', 'jpg']
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/311486.html
上一篇:轉換串列中的每個第n個元素
