我需要转换嵌套字典,如:
log={
'hKtN': {
'edit_field': ['id','coord'],
'ids': {
'4A': {
'update_type': 'delete'
},
'1A': {
'update_type': '',
'update_detail': []
},
'2A': {
'update_type': 'edit',
'update_detail': ['0913','0914']
}
}
}
}
在列表中:
table = [ ['hKtN',['id','coord'],'4A','delete',''],
['hKtN',['id','coord'],'1A','','']
['hKtN',['id','coord'],'2A','edit',['0913','0914']]
]
我将从中构建一个如下表:
logId | edit_field | ids | update_type | update_detail
--------------------------------------------------------------
hKtN | ['id','coord'] | 4A | delete |
hKtN | ['id','coord'] | 1A | |
hKtN | ['id','coord'] | 2A | edit |['0913','0914']
我不能用熊猫。做这件事最像Python的方式是什么
编辑 @Ajax1234的代码运行良好,但非常复杂,需要使用Pandas来构建表。如果可以帮助我构建一个更简单的日志字典,如:
log2={
'logId': 'hKtN',
'edit_field': [
'id',
'coord'
],
'ids': {
'4A': {
'update_type': 'delete'
},
'1A': {
'update_type': '',
'update_detail': [
]
},
'2A': {
'update_type': 'edit',
'update_detail': [
'0913',
'0914'
]
}
}
}
首先,我想说:
这更简单,可以完成这项工作
可以使用递归生成器函数:
输出:
相关问题 更多 >
编程相关推荐