繁体   English   中英

如何遍历嵌套字典并返回键和值?

[英]How to iterate through a nested dictionary and return the keys and values?

假设我有一个这样的嵌套字典:

dict = ["{'model': 'network.customer', 'pk': 'C00001', 'fields': {'name': 'Valentino Solomon', 'latitude': 57.13514, 'longitude': -2.11731}}" 
"{'model': 'network.customer', 'pk': 'C00002', 'fields': {'name': 'Luna Armstrong', 'latitude': 57.13875, 'longitude': -2.09089}}" 
"{'model': 'network.customer', 'pk': 'C00003', 'fields': {'name': 'Jaylen Crane', 'latitude': 57.101, 'longitude': -2.1106}}" 
"{'model': 'network.customer', 'pk': 'C00004', 'fields': {'name': 'Christopher Fritz', 'latitude': 57.10801, 'longitude': -2.23776}}" 
"{'model': 'network.customer', 'pk': 'C00005', 'fields': {'name': 'Timothy Hutchinson', 'latitude': 57.10076, 'longitude': -2.27073}}" 
"{'model': 'network.customer', 'pk': 'C00006', 'fields': {'name': 'Yesenia Reeves', 'latitude': 57.13868, 'longitude': -2.16525}}" 
"{'model': 'network.customer', 'pk': 'C00007', 'fields': {'name': 'Cameron Vargas', 'latitude': 57.16115, 'longitude': -2.15543}}"]

我如何遍历它以获取字段pk以及键fields内的键和值,以便返回:

data = "{'pk': 'C00001', 'name': 'Valentino Solomon', 'latitude': 57.13514, 'longitude': -2.11731}",
"{'pk': 'C00002', 'name': 'Luna Armstrong', 'latitude': 57.13875, 'longitude': -2.09089}",
"{'pk': 'C00003', 'name': 'Jaylen Crane', 'latitude': 57.101, 'longitude': -2.1106}",
"{'pk': 'C00004', 'name': 'Christopher Fritz', 'latitude': 57.10801, 'longitude': -2.23776}",
"{'pk': 'C00005', 'name': 'Timothy Hutchinson', 'latitude': 57.10076, 'longitude': -2.27073}"

谢谢!

我可以使用以下方法访问这些字段:

print(customers[0]['fields'])

将该 dict 转换为一个列表,并使每个条目成为一个 dict。 然后只需遍历列表,将其放入字典中并像访问其他字典一样访问它。

list: list = [{'model': 'network.customer', 'pk': 'C00001', 'fields': {'name': 'Valentino Solomon', 'latitude': 57.13514, 'longitude': -2.11731}}, 
{'model': 'network.customer', 'pk': 'C00002', 'fields': {'name': 'Luna Armstrong', 'latitude': 57.13875, 'longitude': -2.09089}}, 
{'model': 'network.customer', 'pk': 'C00003', 'fields': {'name': 'Jaylen Crane', 'latitude': 57.101, 'longitude': -2.1106}}, 
{'model': 'network.customer', 'pk': 'C00004', 'fields': {'name': 'Christopher Fritz', 'latitude': 57.10801, 'longitude': -2.23776}}, 
{'model': 'network.customer', 'pk': 'C00005', 'fields': {'name': 'Timothy Hutchinson', 'latitude': 57.10076, 'longitude': -2.27073}}, 
{'model': 'network.customer', 'pk': 'C00006', 'fields': {'name': 'Yesenia Reeves', 'latitude': 57.13868, 'longitude': -2.16525}}, 
{'model': 'network.customer', 'pk': 'C00007', 'fields': {'name': 'Cameron Vargas', 'latitude': 57.16115, 'longitude': -2.15543}}]

for entry in list:
    dict: dict = entry
    print(dict['pk'])
    print(dict['fields']['name'])

如果有人有类似 OP 的列表,其中所有 dicts 都在一个字符串中(将不带逗号的 dicts 字符串转换为 dicts 列表),您可以使用以下代码:import ast

def convertToList(inString: str):
    i: int = 0
    closeCounter: int = 0
    openCounter: int = 0
    firstOpen: int = 0
    outList: list = []
    while i < len(inString):
        openPos = inString.find("{", i)
        closePos = inString.find("}", i)
        if closePos == -1:
            return outList
        if openPos < closePos and openPos != -1:
            openCounter += 1
            if openCounter == 1:
                firstOpen = i
            i = openPos + 1
        elif closePos < openPos or openPos == -1:
            closeCounter += 1
            if openCounter == closeCounter:
                dict: dict = ast.literal_eval(inString[firstOpen:closePos+1])
                outList.append(dict)
                openCounter = 0
                closeCounter = 0
            i = closePos + 1
    return outList

                

list: list = ["{'model': 'network.customer', 'pk': 'C00001', 'fields': {'name': 'Valentino Solomon', 'latitude': 57.13514, 'longitude': -2.11731}}" 
"{'model': 'network.customer', 'pk': 'C00002', 'fields': {'name': 'Luna Armstrong', 'latitude': 57.13875, 'longitude': -2.09089}}" 
"{'model': 'network.customer', 'pk': 'C00003', 'fields': {'name': 'Jaylen Crane', 'latitude': 57.101, 'longitude': -2.1106}}" 
"{'model': 'network.customer', 'pk': 'C00004', 'fields': {'name': 'Christopher Fritz', 'latitude': 57.10801, 'longitude': -2.23776}}" 
"{'model': 'network.customer', 'pk': 'C00005', 'fields': {'name': 'Timothy Hutchinson', 'latitude': 57.10076, 'longitude': -2.27073}}" 
"{'model': 'network.customer', 'pk': 'C00006', 'fields': {'name': 'Yesenia Reeves', 'latitude': 57.13868, 'longitude': -2.16525}}" 
"{'model': 'network.customer', 'pk': 'C00007', 'fields': {'name': 'Cameron Vargas', 'latitude': 57.16115, 'longitude': -2.15543}}"]

list0 = convertToList(list[0])
print(list0)
for entry in list0:
    dict: dict = entry
    print(dict['pk'])
    print(dict['fields']['name'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM