一种快速提取和更新大型词典中词条的方法

2024-05-15 02:56:10 发布

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

我有一个包含数千个项目的过期字典和一个包含数千个项目的更新列表,存储为包含要在过期字典中更新的功能的字典,尽管更新列表通常较小(但并不总是)。你知道吗

目前,我正在使用两个for循环遍历旧字典和更新列表,将旧字典中的项与更新列表的正确成员进行匹配,然后覆盖已更新的旧字典成员的相应部分(不是全部k,值对将被更新,并且所有的值对永远不会列在更新的成员中)。你知道吗

这是我的代码(编辑的):

 import copy

json_lyr = {"features": [{"geometry": {"y": 92093.79407307506, "x": 1042519.5130774938},
                          "attributes": {"WildlifeSi": " ", "EditDate": 1462554757161, "Inlet_Elevation": None,
                                         "COVER_DEPT": 0, "Comments": " ", "UpBankfu_4": 0, "SteepRiffl": " ",
                                         "HeadwallMa": " ", "UpBankfu_3": 0, "UpBankfu_2": 0, "CulvertDro": 0,
                                         "OUTFLOW_BO": " ", "RoadkillWi": " ", "DnLtDomina": " ", "CrossingTy": " ",
                                         "UpBankfull": 0, "UPDATE_COM": " ", "LOCAL_REVI": " ", "RefBankf_4": 0,
                                         "RefBankf_2": 0, "RefBankf_3": 0, "RefBankf_1": 0, "CrossingCo": " ",
                                         "Flow_Condi": " ", "ID": 2209, "DnDepositT": " ", "WildlifeIn": " ",
                                         "Crossing_slope": 0, "DnDimensioB": 0, "PIPE_SPALL": " ", "RoadType": " ",
                                         "Wall_Rise": None, "DnBedrockP": " ", "UpBankArmo": " ", "DnDeposits": " ",
                                         "USER_ID": "JUNK", "Tp_Pipe_Out": None, "Wingwall_Angle": None,
                                         "AvulsionDi": 0, "Editor": "cdowd_NHSADES", "CrossDepth": " ", "UpBankfu_1": 0,
                                         "Geomorph_1": " ", "PoolDepthM": 0, "DnRtDomina": " ", "DnDimensioD": 0,
                                         "DnDimensioC": 0, "INSTALLATI": None, "DnDimensioA": 0, "UpUndermin": " ",
                                         "UpDeposits": " ", "STEEPER_WI": " ", "OUTFALL_TR": " ", "PoolDepthS": 0,
                                         "StructureM": " ", "StructureL": 0, "SED_BUILDU": " ", "OUTLET_H_1": " ",
                                         "JOINT_SEPE": " ", "DnUndermin": " ", "RAILROAD_C": " ", "UpDepositT": " ",
                                         "UpDimensio": 0, "StructureU": " ", "StructureS": " ", "UpEviEroAg": " ",
                                         "Town": " ", "CulvertWat": 0, "FloodPlain": " ", "ASSE_DATE": 1433289600000,
                                         "DnBedMater": " ", "ApproachAn": " ", "PIPE_DEFOR": " ", "Tp_pipe_Inlet": None,
                                         "DnEvidEroA": " ", "UpLtVegeta": " ", "HydCntrlTy": " ", "STORM_DRAI": " ",
                                         "Dn_Side_Ele": None, "UpBeaverDa": " ", "T2_REVIEW": " ", "PoolDepth4": " ",
                                         "DnBeaver_1": 0, "InSubstrat": " ", "SCREENING_": " ", "ChannelAvu": " ",
                                         "DRIVEWAY_C": " ", "INLET_HEAD": " ", "UpRtVegeta": " ", "MARKER_POS": " ",
                                         "HighDownst": " ", "DnBankEros": " ", "AOPCompat": " ", "DistToHydC": 0,
                                         "CulvertSlo": " ", "DnBankArmo": " ", "Observers": " ", "Dn_Hydr_Elev": None,
                                         "POINT_X": 1042519.51307749, "POINT_Y": 92093.7940730751, "Up_Side_Ele": None,
                                         "Road_Elevation": None, "DES_REVIEW": " ", "OUTLET_HEI": " ",
                                         "InDepositT": " ", "OBJECTID": 1, "UpBedMater": " ", "OUTLET_HEA": " ",
                                         "InBedMat_1": " ", "PIPE_CORRO": " ", "UpRtDomina": " ", "SCOUR_OUTL": " ",
                                         "InDeposits": " ", "NoShoulder": 0, "CULVERT_OU": " ",
                                         "Creator": "cdowd_NHSADES", "RefBankful": 0, "SteppedFoo": " ",
                                         "Pool_Btm_Elev": None, "PAINTED_MA": " ", "DnBankFull": 0, "UpBedrockP": " ",
                                         "StreamName": " ", "DnRtVegeta": " ", "Hyd_Cont_Elev": None,
                                         "GlobalID_2": "8bb7eb9e-45b2-4e16-b563-f2a44787342b", "UpBeaver_1": 0,
                                         "Outlet_Elevation": None, "Dn_to_Hydr_Cont": 0, "WildlifeUp": " ",
                                         "ROAD_NAME": " ", "StructureCount": 0, "UpLtDomina": " ", "Downstream": " ",
                                         "CulvertOverflow": " ", "Inlet_Type": None, "INFLOW_BOD": " ", "TIDAL": " ",
                                         "DnBeaverDa": " ", "CrossVelMa": " ", "UpBankEros": " ", "DnLtVegeta": " ",
                                         "UPDATE_OBS": " ", "DnBankFu_4": 0, "InBedMater": " ", "DnBankFu_1": 0,
                                         "DnBankFu_2": 0, "DnBankFu_3": 0, "RECENT_RAI": " ",
                                         "CreationDate": 1462554757161,
                                         "GlobalID": "{e5f9d928-c9c2-4c43-b7de-eeefa7820955}", "NoTravelLa": 0,
                                         "UpDimens_2": 0, "UpDimens_3": 0, "REVIEW_LEV": "Incomplete", "UpDimens_1": 0,
                                         "Jurisdicti": " ", "UPDATE_DAT": None, "EMBEDDED_D": 0, "CULVERT_IN": " ",
                                         "GeomorphCo": " "}},
                         {"geometry": {"y": 157642.015010491, "x": 1164940.328930911},
                          "attributes": {"WildlifeSi": " ", "EditDate": 1462554757161, "Inlet_Elevation": None,
                                         "COVER_DEPT": 0.25, "Comments": " ", "UpBankfu_4": 9, "SteepRiffl": "No",
                                         "HeadwallMa": "Concrete", "UpBankfu_3": 6, "UpBankfu_2": 6.5, "CulvertDro": 0,
                                         "OUTFLOW_BO": "Wetland", "RoadkillWi": "Goupher",
                                         "DnLtDomina": "Herbaceous/grass", "CrossingTy": "Box Culvert",
                                         "UpBankfull": 9.5,
                                         "UPDATE_COM": "1) Wetland present. Would argue that geomorphic compatibility determination is inappropriate in this instance. 2) What is the substrate type of material (if exists) in the structure? ",
                                         "LOCAL_REVI": " ", "RefBankf_4": 3, "RefBankf_2": 5, "RefBankf_3": 3.25,
                                         "RefBankf_1": 8, "CrossingCo": "Old", "Flow_Condi": "Typically Low",
                                         "ID": 2210, "DnDepositT": "None", "WildlifeIn": " ", "Crossing_slope": 0,
                                         "DnDimensioB": 4.1, "PIPE_SPALL": "Low", "RoadType": "Paved",
                                         "Wall_Rise": None, "DnBedrockP": "No", "UpBankArmo": "None", "DnDeposits": " ",
                                         "USER_ID": "EKINNorthRd01", "Tp_Pipe_Out": None, "Wingwall_Angle": None,
                                         "AvulsionDi": 40, "Editor": "cdowd_NHSADES", "CrossDepth": "Yes",
                                         "UpBankfu_1": 5.5, "Geomorph_1": " ", "PoolDepthM": 0,
                                         "DnRtDomina": "Shrub/sapling", "DnDimensioD": 0, "DnDimensioC": 0,
                                         "INSTALLATI": 1433808000000, "DnDimensioA": 6, "UpUndermin": "None",
                                         "UpDeposits": " ", "STEEPER_WI": "No", "OUTFALL_TR": "None", "PoolDepthS": 0,
                                         "StructureM": "Concrete", "StructureL": 35, "SED_BUILDU": "Open",
                                         "OUTLET_H_1": "Good", "JOINT_SEPE": "None", "DnUndermin": "None",
                                         "RAILROAD_C": "No", "UpDepositT": "None", "UpDimensio": 6,
                                         "StructureU": "None", "StructureS": "No", "UpEviEroAg": "None",
                                         "Town": "East Kingston", "CulvertWat": 4.5, "FloodPlain": "Not Significant",
                                         "ASSE_DATE": 1433808000000, "DnBedMater": "Cobble",
                                         "ApproachAn": "Naturally Straight", "PIPE_DEFOR": " ", "Tp_pipe_Inlet": None,
                                         "DnEvidEroA": "None", "UpLtVegeta": "Yes", "HydCntrlTy": " ",
                                         "STORM_DRAI": "No", "Dn_Side_Ele": None, "UpBeaverDa": "No", "T2_REVIEW": " ",
                                         "PoolDepth4": " ", "DnBeaver_1": 0, "InSubstrat": "NA", "SCREENING_": "No",
                                         "ChannelAvu": "Cross & Follow Road", "DRIVEWAY_C": "No", "INLET_HEAD": "Good",
                                         "UpRtVegeta": "Yes", "MARKER_POS": "NA", "HighDownst": "No",
                                         "DnBankEros": "None", "AOPCompat": " ", "DistToHydC": 0,
                                         "CulvertSlo": "About the Same", "DnBankArmo": "None", "Observers": "RPC_NLCW",
                                         "Dn_Hydr_Elev": None, "POINT_X": 1164965.05788408, "POINT_Y": 157629.355258904,
                                         "Up_Side_Ele": None, "Road_Elevation": None, "DES_REVIEW": " ",
                                         "OUTLET_HEI": "< 1 ft above Channel", "InDepositT": "None", "OBJECTID": 2,
                                         "UpBedMater": "Cobble", "OUTLET_HEA": "Concrete", "InBedMat_1": " ",
                                         "PIPE_CORRO": " ", "UpRtDomina": "Shrub/sapling", "SCOUR_OUTL": "None",
                                         "InDeposits": " ", "NoShoulder": 0, "CULVERT_OU": "At Grade",
                                         "Creator": "cdowd_NHSADES", "RefBankful": 7, "SteppedFoo": "No",
                                         "Pool_Btm_Elev": None, "PAINTED_MA": "NA", "DnBankFull": 7.33,
                                         "UpBedrockP": "No",
                                         "StreamName": "NHRIV600030805-04 GREAT BROOK - BRICKYARD BROOK",
                                         "DnRtVegeta": "Yes", "Hyd_Cont_Elev": None,
                                         "GlobalID_2": "ce43e49c-5386-4127-8db9-793228b03c13", "UpBeaver_1": 0,
                                         "Outlet_Elevation": None, "Dn_to_Hydr_Cont": 0, "WildlifeUp": "Bugs, birds",
                                         "ROAD_NAME": "Rt 108 north rd", "StructureCount": 1,
                                         "UpLtDomina": "Herbaceous/grass", "Downstream": "No", "CulvertOverflow": "No",
                                         "Inlet_Type": None, "INFLOW_BOD": "Wetland", "TIDAL": "No", "DnBeaverDa": "No",
                                         "CrossVelMa": "Yes", "UpBankEros": "None", "DnLtVegeta": "Yes",
                                         "UPDATE_OBS": " ", "DnBankFu_4": 3.5, "InBedMater": "No", "DnBankFu_1": 5.9,
                                         "DnBankFu_2": 7.75, "DnBankFu_3": 5, "RECENT_RAI": "Within 1 week",
                                         "CreationDate": 1462554757161,
                                         "GlobalID": "{89bc7cfd-b115-42ca-aa07-16ce42539c4d}", "NoTravelLa": 2,
                                         "UpDimens_2": 0, "UpDimens_3": 0, "REVIEW_LEV": "Incomplete",
                                         "UpDimens_1": 4.1, "Jurisdicti": "NH DOT", "UPDATE_DAT": None, "EMBEDDED_D": 0,
                                         "CULVERT_IN": "At Grade", "GeomorphCo": " "}}]}

update_list = [{'ID': 2209, 'GeomorphCo': 'Fail'},
               {'ID': 2210, 'GlobalID': '{e5f9d928-c9c2-4c43-b7de-eeefa7820955}'}]

updated_features_superlist = []

for feature in json_lyr['features']:
    for update_feature in update_list:
        # if the IDs match
        if feature['attributes']['ID'] == update_feature['ID']:
            updated_feature = copy.deepcopy(feature)
            for key, value in update_feature.items():
                if key == 'ID':
                    continue  # don't need to update this
                else:
                    updated_feature['attributes'][key] = update_feature[key]
            # adding to the list of features that are updated
            updated_features_superlist.append(updated_feature)

其中json_lyr是我的过时字典,update_list是我的更新特性列表。你知道吗

由于过时字典和字典更新列表中都有数千个点,因此需要几分钟才能完成2for循环。有没有一个快速的方法来建立一个更新的功能列表?你知道吗

而且,每次运行脚本时,需要更新的属性(键、值对)都有很大的不同,从1个属性到几十个属性——因此,在update\u列表中存储的字典中通过键、值对进行迭代。你知道吗

编辑:

过期功能示例:

{
    'geometry': {
        'y': 104288.95701865852,
        'x': 854862.8250714131
    },
    'attributes': {
        'WildlifeSi': None,
        'EditDate': None,
        'Inlet_Elevation': 0,
        'COVER_DEPT': 1.89999997615814,
        'Comments': None,
        'UpBankfu_4': None,
        'SteepRiffl': None,
        'HeadwallMa': 'DryFitStone',
        'UpBankfu_3': None,
        'UpBankfu_2': None,
        'CulvertDro': None,
        'OUTFLOW_BO': 'Ditch',
        'RoadkillWi': 'None',
        'DnLtDomina': None,
        'CrossingTy': 'RoundCulvert',
        'UpBankfull': None,
        'UPDATE_COM': None,
        'LOCAL_REVI': None,
        'RefBankf_4': None,
        'RefBankf_2': None,
        'RefBankf_3': None,
        'RefBankf_1': None,
        'CrossingCo': 'Old',
        'Flow_Condi': None,
        'SADES_ID': 6574,
        'DnDepositT': None,
        'WildlifeIn': None,
        'Crossing_slope': 1,
        'DnDimensioB': 0,
        'PIPE_SPALL': 'NotRated',
        'RoadType': 'Paved',
        'Wall_Rise': 0,
        'DnBedrockP': None,
        'UpBankArmo': None,
        'DnDeposits': None,
        'USER_ID': None,
        'Tp_Pipe_Out': 0,
        'Wingwall_Angle': 'NoneofThese',
        'AvulsionDi': None,
        'Editor': None,
        'CrossDepth': None,
        'UpBankfu_1': None,
        'Geomorph_1': None,
        'PoolDepthM': None,
        'DnRtDomina': None,
        'DnDimensioD': 0,
        'DnDimensioC': 0,
        'INSTALLATI': None,
        'DnDimensioA': 0.930000007152557,
        'UpUndermin': 'None',
        'UpDeposits': None,
        'STEEPER_WI': None,
        'OUTFALL_TR': 'None',
        'PoolDepthS': None,
        'StructureM': 'Plastic-Smooth',
        'StructureL': 29.8299999237061,
        'SED_BUILD': '1/4Plugged',
        'OUTLET_H_1': 'Fair',
        'JOINT_SEPE': 'None',
        'DnUndermin': 'None',
        'RAILROAD_C': 'No',
        'UpDepositT': None,
        'UpDimensio': 0.980000019073486,
        'Structure': 'None',
        'StructureS': 'No',
        'UpEviEroAg': None,
        'Town': 'Fitzwilliam',
        'CulvertWat': None,
        'FloodPlain': None,
        'ASSE_DATE': 1469033829000L,
        'DnBedMater': None,
        'ApproachAn': None,
        'PIPE_DEFOR': 'None',
        'Tp_pipe_Inlet': 0,
        'DnEvidEroA': None,
        'UpLtVegeta': None,
        'HydCntrlTy': None,
        'STORM_DRAI': 'Yes',
        'Dn_Side_Ele': 0,
        'UpBeaverDa': 'No',
        'T2_REVIEW': None,
        'PoolDepth4': None,
        'DnBeaver_1': None,
        'InSubstrat': None,
        'SCREENING_': 'No',
        'ChannelAv': None,
        'DRIVEWAY_C': 'No',
        'INLET_HEAD': 'Fair',
        'UpRtVegeta': None,
        'MARKER_POS': 'NA',
        'HighDownst': None,
        'DnBankEros': None,
        'AOPCompat': '',
        'DistToHydC': None,
        'CulvertSlo': None,
        'DnBankArmo': None,
        'Observers': 'SWRPCMCHG',
        'Dn_Hydr_Elev': None,
        'POINT_X': None,
        'POINT_Y': None,
        'Up_Side_Ele': 0,
        'Road_Elevation': 0,
        'DES_REVIEW': None,
        'OUTLET_HEI': 'AtGrade',
        'InDepositT': None,
        'OBJECTID': 6994,
        'UpBedMater': None,
        'OUTLET_HEA': 'DryFitStone',
        'InBedMat_1': None,
        'PIPE_CORRO': 'NotRated',
        'UpRtDomina': None,
        'SCOUR_OUTL': 'None',
        'InDeposits': None,
        'NoShoulder': 0,
        'CULVERT_O': None,
        'Creator': None,
        'RefBankful': None,
        'SteppedFoo': None,
        'Pool_Btm_Elev': None,
        'PAINTED_MA': 'NA',
        'DnBankFull': None,
        'UpBedrockP': None,
        'StreamName': 'NA',
        'DnRtVegeta': None,
        'Hyd_Cont_Elev': None,
        'GlobalID_2': '0544fd95-aee7-4874-9e03-fb8800b51da4',
        'UpBeaver_1': None,
        'Outlet_Elevation': None,
        'Dn_to_Hydr_Cont': None,
        'WildlifeUp': None,
        'ROAD_NAME': 'RobbinsRoad',
        'StructureCount': 1,
        'UpLtDomina': None,
        'Downstream': None,
        'CulvertOverflow': 'No',
        'Inlet_Type': 'NoneofThese',
        'INFLOW_BOD': 'Ditch',
        'TIDAL': 'No',
        'DnBeaverDa': None,
        'CrossVelMa': None,
        'UpBankEros': None,
        'DnLtVegeta': None,
        'UPDATE_OBS': None,
        'DnBankFu_4': None,
        'InBedMater': None,
        'DnBankFu_1': None,
        'DnBankFu_2': None,
        'DnBankFu_3': None,
        'RECENT_RAI': 'Within1week',
        'CreationDate': None,
        'GlobalID': None,
        'NoTravelLa': 2,
        'UpDimens_2': 0,
        'UpDimens_3': 0,
        'REVIEW_LEV': 'QAisN/A',
        'UpDimens_1': 0,
        'Jurisdicti': 'Municipal',
        'UPDATE_DAT': None,
        'EMBEDDED_D': 0,
        'CULVERT_IN': None,
        'GeomorphCo': ''
    }
}

功能的更新列表示例-在这种情况下,只需要更新一个属性:

{'SADES_ID': 6574, 'GeomorphCo': 'Fail'}

Tags: nononeid列表字典updatefeaturedn
1条回答
网友
1楼 · 发布于 2024-05-15 02:56:10

首先,我将创建一个字典映射ID到update_list中的项:

update_mapping = dict((update_feature['ID'], update_feature)
                      for update_feature in update_list)

然后运行循环,但使用字典查找而不是for循环来获取update_feature

for feature in json_lyr['layers'][0]['features']:
    if feature['attributes']['ID'] in update_mapping:
        update_feature = update_mapping[feature['attributes']['ID']]
        updated_feature = copy.deepcopy(feature)
        for key, value in update_feature.items():
            if key == 'ID': continue        # don't need to update this
            else:
                updated_feature['attributes'][key] = update_feature[key]
        # adding to the list of features that are updated
        updated_features_superlist.append(updated_feature)

相关问题 更多 >

    热门问题