[英]How to iterate through a nested dictionary and return the keys and values?
Say I have a nested dictionary like so:假设我有一个这样的嵌套字典:
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}}"]
How can I iterate through this to get the fields pk
and the keys and values inside the key fields
so that the return is:我如何遍历它以获取字段
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}"
Thanks!谢谢!
I can access the fields by using this:我可以使用以下方法访问这些字段:
print(customers[0]['fields'])
Convert that dict into a list and make every entry a dict.将该 dict 转换为一个列表,并使每个条目成为一个 dict。 Then just iterate through the list, put it in a dict and access it like you would every other 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'])
If anybody has a similar list like OP, where all the dicts are in one single string (convert string of dicts without commas into list of dicts), you can use this bit of code: import ast如果有人有类似 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.