社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何将字典中的一些键分组到python中的嵌套键?

hanzgs • 3 年前 • 1135 次点击  

我有一本字典清单

[{
'LoanID' : 001,
'Applicant_Income.1' : 5000,
'Applicant_Occupation.1' : "Manager",
'Applicant_Income.2' : 3000,
'Applicant_Occupation.2' : "Accountant",
'LoanAmount' : 10000
},
{
'LoanID' : 002,
'Applicant_Income.1' : 6000,
'Applicant_Occupation.1' : "Senior Manager",
'Applicant_Income.2' : 2000,
'Applicant_Occupation.2' : "Services",
'LoanAmount' : 8000
}]

我需要创建一个新的键“申请人”,并将“申请人收入”和“申请人职业”移到其中,结果列表中的每个dict如下所示

{
'LoanID' : 001,
'Applicant' : [
     { 
       'Applicant_Income' : 5000,
       'Applicant_Occupation' : "Manager"
     }'
     {
       'Applicant_Income' : 3000,
       'Applicant_Occupation' : "Accountant"
     }]
'LoanAmount' : 10000
},
{
'LoanID' : 002,
'Applicant' : [
     { 
       'Applicant_Income' : 6000,
       'Applicant_Occupation' : "Senior Manager"
     }'
     {
       'Applicant_Income' : 2000,
       'Applicant_Occupation' : "Services"
     }]
'LoanAmount' : 8000
}
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128389
 
1135 次点击  
文章 [ 2 ]  |  最新文章 3 年前
David
Reply   •   1 楼
David    3 年前

另一个答案只有在收入和职业值的数量固定为2时才有效。

下面是一个更加动态的解决方案:
组织列表 是你在上面定义的列表吗
终结者 这是你想要的清单吗

def getApplicant(entry):
    tempAppList = []
    for key in [key for key, value in entry.items() if 'Applicant_Income' in key]:
        tempAppDict = {}
        tempAppDict['Applicant_Income'] = entry[key]
        tempAppDict['Applicant_Occupation'] = entry[key.replace('Income', 'Occupation')]
        tempAppList.append(tempAppDict)
    
    return tempAppList

finalList = []

for entry in orgList:
    tempDict = {}
    tempDict['LoanID'] = entry['LoanID']
    tempDict['Applicant'] = getApplicant(entry)
    tempDict['LoanAmount'] = entry['LoanAmount']
    
    finalList.append(tempDict)

输出:

[
    {
        "LoanID": "001",
        "Applicant": [
            {
                "Applicant_Income": 5000,
                "Applicant_Occupation": "Manager"
            },
            {
                "Applicant_Income": 3000,
                "Applicant_Occupation": "Accountant"
            }
        ],
        "LoanAmount": 10000
    },
    {
        "LoanID": "002",
        "Applicant": [
            {
                "Applicant_Income": 6000,
                "Applicant_Occupation": "Senior Manager"
            },
            {
                "Applicant_Income": 2000,
                "Applicant_Occupation": "Services"
            }
        ],
        "LoanAmount": 8000
    }
]

注意:整数上不允许使用前导零(就像LoadId上一样)。所以你需要用一个字符串

catasaurus
Reply   •   2 楼
catasaurus    3 年前

这应该管用( info 是您在上面定义的列表):

for d in range(len(info)):
    info[d]['Applicant'] = [{'Applicant_Income.1': info[d]['Applicant_Income.1'], 'Applicant_Occupation.1': info[d]['Applicant_Occupation.1']}, {'Applicant_Income.2': info[d]['Applicant_Income.2'], 'Applicant_Occupation.2': info[d]['Applicant_Occupation.2']}]
    del info[d]['Applicant_Income.1']
    del info[d]['Applicant_Occupation.1']
    del info[d]['Applicant_Income.2']
    del info[d]['Applicant_Occupation.2']

输出:

[{'LoanID': '002',
  'LoanAmount': 10000,
  'Applicant': [{'Applicant_Income.1': 5000,
    'Applicant_Occupation.1': 'Manager'},
   {'Applicant_Income.2': 3000, 'Applicant_Occupation.2': 'Accountant'}]},
 {'LoanID': '002',
  'LoanAmount': 8000,
  'Applicant': [{'Applicant_Income.1': 6000,
    'Applicant_Occupation.1': 'Senior Manager'},
   {'Applicant_Income.2': 2000, 'Applicant_Occupation.2': 'Services'}]}]