现在,我的JSON数据库如下所示:
[{'srcStationName': 'Central', 'srcStationID': 1, 'dstName': 'Central',
'dstID': 1, 'octopusAdultFare': 0, 'octopusStudentFare': 0,
'singleAdultFare': 0, 'octopusChildFare': 0, 'octopusElderlyFare': 0,
'octopusDisabledFare': 0, 'singleChildFare': 0, 'singleElderlyFare': 0},
{'srcStationName': 'Central', 'srcStationID': 1, 'dstName': 'Admiralty',
'dstID': 2, 'octopusAdultFare': 4.6, 'octopusStudentFare': 3,
'singleAdultFare': 5, 'octopusChildFare': 3, 'octopusElderlyFare': 2,
'octopusDisabledFare': 2, 'singleChildFare': 3, 'singleElderlyFare': 3},
{'srcStationName': 'Central', 'srcStationID': 1, 'dstName': 'Tsim Sha Tsui',
'dstID': 3, 'octopusAdultFare': 10, 'octopusStudentFare': 5,
'singleAdultFare': 11, 'octopusChildFare': 5, 'octopusElderlyFare': 2,
'octopusDisabledFare': 2, 'singleChildFare': 5, 'singleElderlyFare': 5},
{'srcStationName': 'Heng On', 'srcStationID': 101, 'dstName': 'Fortress
Hill', 'dstID': 30, 'octopusAdultFare': 21.5, 'octopusStudentFare': 10.7,
'singleAdultFare': 24, 'octopusChildFare': 10.7, 'octopusElderlyFare': 2,
'octopusDisabledFare': 2, 'singleChildFare': 12, 'singleElderlyFare': 12},
{'srcStationName': 'Heng On', 'srcStationID': 101, 'dstName': 'North Point',
'dstID': 31, 'octopusAdultFare': 21.5, 'octopusStudentFare': 10.7,
'singleAdultFare': 24, 'octopusChildFare': 10.7, 'octopusElderlyFare': 2,
'octopusDisabledFare': 2, 'singleChildFare': 12, 'singleElderlyFare': 12},
{'srcStationName': 'Heng On', 'srcStationID': 101, 'dstName': 'Quarry Bay',
'dstID': 32, 'octopusAdultFare': 21.5, 'octopusStudentFare': 10.7,
'singleAdultFare': 24, 'octopusChildFare': 10.7, 'octopusElderlyFare': 2,
'octopusDisabledFare': 2, 'singleChildFare': 12, 'singleElderlyFare': 12}]
由于存在相同的“srcStations”和多个“Destination”,因此我希望得到的结果是我希望转换为如下格式:
{1: {1: {'octopusAdultFare': 0, 'octopusStudentFare': 0, 'octopusChildFare':
0, 'octopusElderlyFare': 0, 'octopusDisabledFare': 0, 'singleAdultFare': 0,
'singleChildFare': 0, 'singleElderlyFare': 0}, 2: {'octopusAdultFare': 4.6,
'octopusStudentFare': 3, 'octopusChildFare': 3, 'octopusElderlyFare': 2,
'octopusDisabledFare': 2, 'singleAdultFare': 5, 'singleChildFare': 3,
'singleElderlyFare': 3}, 3: {'octopusAdultFare': 10, 'octopusStudentFare':
5, 'octopusChildFare': 5, 'octopusElderlyFare': 2, 'octopusDisabledFare': 2,
'singleAdultFare': 11, 'singleChildFare': 5, 'singleElderlyFare': 5},
101: {1: {'octopusAdultFare': 21.5, 'octopusStudentFare': 10.7
'octopusChildFare': 10.7, 'octopusElderlyFare': 2, 'octopusDisabledFare':
2, 'singleAdultFare': 24, 'singleChildFare': 12, 'singleElderlyFare': 12},
2: {'octopusAdultFare': 21.5, 'octopusStudentFare': 10.7, 'octopusChildFare':
10.7, 'octopusElderlyFare': 2, 'octopusDisabledFare': 2,
'singleAdultFare': 24, 'singleChildFare': 12, 'singleElderlyFare': 12}, 3: {'octopusAdultFare':
21.5, 'octopusStudentFare': 10.7, 'octopusChildFare': 10.7,
'octopusElderlyFare': 2, 'octopusDisabledFare': 2, 'singleAdultFare':
24, 'singleChildFare': 12, 'singleElderlyFare': 12}
}
其中,第一个“1”和“101”键是“srcStationID”,相应的对象包含“dstIDs”作为键及其价格。我需要帮助以python编写一个函数,该函数将为我执行此转换,并将结果写入JSON文件。多谢各位
我可以使用以下代码完成此操作:
相关问题 更多 >
编程相关推荐