我需要访问字典列表中的所有[Location][LocationCategory]。列表中有421本词典。不断出错

2024-05-06 23:23:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我的字典列表是“atm\U数据”,我当前的代码是:

def atms_location_type(atms_data):
    atms_amount={'BranchExternal':0,'BranchInternal':0,'Other':0,'BranchLobby':0}
    for i in atms_data:
        if [i]['Location']['LocationCategory']=='BranchExternal':
            atms_amount['BranchExternal']+=1
        elif [i]['Location']['LocationCategory']=='BranchInternal':
            atms_amount['BranchInternal']+=1
        elif [i]['Location']['LocationCategory']=='BranchLobby':
            atms_amount['BranchLobby']+=1
        elif [i]['Location']['LocationCategory']=='Other':
            atms_amount['Other']+=1
            
    return (atms_amount)

atms_location_type(atms_data)
atms_amount_clear=atms_location_type(atms_data)

1条回答
网友
1楼 · 发布于 2024-05-06 23:23:21

应该用i替换[i],这里有一个简短的代码

def atms_location_type(atms_data):

    atms_amount={
        'BranchExternal':0,
        'BranchInternal':0,
        'BranchLobby':0,
        'Other':0,
    }

    for item in atms_data:
        tag = item['Location']['LocationCategory']
        if tag not in ['BranchExternal', 'BranchInternal', 'BranchLobby']:
            tag = 'Other'
        atms_amount[tag] += 1

    return atms_amount

相关问题 更多 >