Select to view content in your preferred language

Field mapping for dummies?

891
3
08-29-2013 02:00 PM
KevinBell
Deactivated User
Hi all!  I don't think I'm smart enough to get this arcpy.fieldmapping thing...  Does the following code have any advantages/disadvantages compared to the fieldmapping?  I like this because it seems like a lower level solution.

    d = {}
    flds = ['pk1', 'origFld1', 'origFld2']
    with arcpy.da.SearchCursor(myTbl, flds) as c:
        for r in c:
            d[r[0]] = (r[1], r[2])
    
    flds = ['pk2', 'destFld1', 'destFld2']
    with arcpy.da.UpdateCursor('myFC', flds) as cc:
        for rr in cc:
            if d.has_key(rr[0]):
                rr[1] = d[rr[0]][0]
                rr[2] = d[rr[0]][1]
                rr[3] = d[rr[0]][2]
      
                cc.updateRow(rr)
            else:
                pass
Tags (2)
0 Kudos
3 Replies
ChrisSnyder
Honored Contributor
Advantages: speed, flexibility, awesomeness
Disadvantages: none

... How about in 6 lines?

d = dict([(r[0],(r[1],r[2])) for r in arcpy.da.SearchCursor(myTbl, ['pk1','origFld1','origFld2'])])
with arcpy.da.UpdateCursor('myFC', ['pk2','destFld1','destFld2']) as cc:
    for rr in cc:
        if rr[0] in d:
            rr[1],rr[2],rr[3] = d[rr[0]][0],d[rr[0]][1],d[rr[0]][2]
            cc.updateRow(rr)
0 Kudos
KevinBell
Deactivated User
Advantages: speed, flexibility, awesomeness
Disadvantages: none

... How about in 6 lines?

d = dict([(r[0],(r[1],r[2])) for r in arcpy.da.SearchCursor(myTbl, ['pk1','origFld1','origFld2'])])
with arcpy.da.UpdateCursor('myFC', ['pk2','destFld1','destFld2']) as cc:
    for rr in cc:
        if rr[0] in d:
            rr[1],rr[2],rr[3] = d[rr[0]][0],d[rr[0]][1],d[rr[0]][2]
            cc.updateRow(rr)


Nice tune up!  Methinks this is more pythonic for sure!
0 Kudos
by Anonymous User
Not applicable
Advantages: speed, flexibility, awesomeness
Disadvantages: none

... How about in 6 lines?

d = dict([(r[0],(r[1],r[2])) for r in arcpy.da.SearchCursor(myTbl, ['pk1','origFld1','origFld2'])])
with arcpy.da.UpdateCursor('myFC', ['pk2','destFld1','destFld2']) as cc:
    for rr in cc:
        if rr[0] in d:
            rr[1],rr[2],rr[3] = d[rr[0]][0],d[rr[0]][1],d[rr[0]][2]
            cc.updateRow(rr)


Chris Snyder is officially the king of efficiency....
0 Kudos