python强大的数据类型转换

#  原始的二维表数据集
jsonObj=[
    {"bureau_id":1001,"bureau_name":"一中","person_id":11001},
    {"bureau_id":1001,"bureau_name":"一中","person_id":11002},
    {"bureau_id":1001,"bureau_name":"一中","person_id":11003},
    {"bureau_id": 1002, "bureau_name": "二中", "person_id": 21001},
    {"bureau_id": 1002, "bureau_name": "二中", "person_id": 21002},
    {"bureau_id": 1002, "bureau_name": "二中", "person_id": 21003},
    {"bureau_id": 1003, "bureau_name": "三中", "person_id": 31001},
    {"bureau_id": 1003, "bureau_name": "三中", "person_id": 31002},
    {"bureau_id": 1003, "bureau_name": "三中", "person_id": 31003}
]

# 结构化的数据集
personDict={}
for obj in jsonObj:
    if 'bureau_'+str(obj['bureau_id']) not in personDict:
        personDict['bureau_'+str(obj['bureau_id'])]={'bureau_name':obj['bureau_name']}

    if 'personIds' not in  personDict['bureau_' + str(obj['bureau_id'])]:
        personDict['bureau_' + str(obj['bureau_id'])]['personIds']=[]
    personDict['bureau_' + str(obj['bureau_id'])]['personIds'].append(obj['person_id'])


for c in personDict:
    # 群组id
    print(c)
    # 群组名
    print(personDict[c]['bureau_name'])
    # 有哪些人员
    print(personDict[c]['personIds'])
    print('====================================')

猜你喜欢

转载自www.cnblogs.com/littlehb/p/8921884.html