繁体   English   中英

遍历嵌套字典

[英]Loop through nested dictionary

我有嵌套字典,如下所示:

{
'location': {0: 'London', 1: 'London', 2: 'London', 3: 'London', 4: 'London', 5: 'London', 6: 'London'}, 
'attraction': {0: 'museum', 1: 'museum', 2: 'museum', 3: 'museum', 4: 'museum', 5: 'museum', 6: 'museum'},
'name': {0: 'London Museum', 1: 'British Museum', 2: 'Natural History Museum', 3: 'London's Docklands Museum', 4: 'Science Museum  ', 5: 'London Transport Museum ', 6: 'Victoria and Albert Museum'},
'rate': {0: 4.6, 1: 4.7, 2: 4.7, 3: 4.6, 4: 4.5, 5: 4.5, 6: 4.7}, 
'totalRates': {0: 13873, 1: 115863, 2: 13497, 3: 4528, 4: 51675, 5: 7349, 6: 42541},
'tag': {0: 'Muzeum', 1: 'Muzeum', 2: 'Muzeum', 3: 'Muzeum', 4: 'Muzeum', 5: 'Muzeum', 6: 'Muzeum'},
'address': {0: '150 London Wall', 1: 'Great Russell St', 2: 'Cromwell Rd', 3: '1 Warehouse. West India Quay. No. Hertsmere Rd', 4: 'Exhibition Rd', 5: 'Stare autobusy. pociągi i tramwaje Londynu', 6: 'Cromwell Rd'},
'description': {0: 'Some description', 1: 'Some description', 2: 'Some description', 3: 'Some description', 4: 'Some description', 5: 'Some description', 6: 'Some description'},
'image': {0: 'https://someImage.com/something', 1: 'https://someImage.com/something', 2: 'https://someImage.com/something', 3: 'https://someImage.com/something', 4: 'https://someImage.com/something', 5: 'https://someImage.com/something', 6: 'https://someImage.com/something'}
}

我想遍历并获取有关每个元素的信息,如下所示:

{
'location': 'London',
'attraction': 'museum',
'name': 'London Museum',
'rate': 4.6,
'totalRates': 13873,
'tag': 'Muzeum',
'address': '150 London Wall',
'description': 'Some description',
'image': 'https://someImage.com/something'
},
{
'location': 'London',
'attraction': 'museum',
'name': 'British Museum',
'rate': 4.7,
'totalRates': 115863,
'tag': 'Muzeum',
'address': 'Great Russell St',
'description': 'Some description',
'image': 'https://someImage.com/something'
},
ect...

我获得这个嵌套字典的方法是使用.to_dict()方法转换pandas.DataFrame

是否有可能通过循环或转换 dataframe 以任何不同的方式获得上述结果? 我需要将结果作为字典,以便通过django上下文显示数据。

您需要在to_dict()中指定orient=records

>>> df.to_dict("records")

[{'location': 'London',
  'attraction': 'museum',
  'name': 'London Museum',
  'rate': 4.6,
  'totalRates': 13873,
  'tag': 'Muzeum',
  'address': '150 London Wall',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'British Museum',
  'rate': 4.7,
  'totalRates': 115863,
  'tag': 'Muzeum',
  'address': 'Great Russell St',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'Natural History Museum',
  'rate': 4.7,
  'totalRates': 13497,
  'tag': 'Muzeum',
  'address': 'Cromwell Rd',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': "London's Docklands Museum",
  'rate': 4.6,
  'totalRates': 4528,
  'tag': 'Muzeum',
  'address': '1 Warehouse. West India Quay. No. Hertsmere Rd',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'Science Museum  ',
  'rate': 4.5,
  'totalRates': 51675,
  'tag': 'Muzeum',
  'address': 'Exhibition Rd',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'London Transport Museum ',
  'rate': 4.5,
  'totalRates': 7349,
  'tag': 'Muzeum',
  'address': 'Stare autobusy. pociągi i tramwaje Londynu',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'Victoria and Albert Museum',
  'rate': 4.7,
  'totalRates': 42541,
  'tag': 'Muzeum',
  'address': 'Cromwell Rd',
  'description': 'Some description',
  'image': 'https://someImage.com/something'}]

假设您有一个名为“字典”的变量中的数据,这样的事情应该可以工作。

element_dicts = []
for i in range(len(dictionary['location'].keys())):
    data = dict()
    for each in range(len(dictionary.keys())):
         data[each] = dictionary[each][i]
    element_dicts.append(data)

“element_dicts”现在将是一个列表,其中包含第 i 个索引处每个位置的字典。

nm = list(d.keys())
n = len(d[nm[0]])
[{k: d[k][i] for k in nm} for i in range(n)]

暂无
暂无

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

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