0
import arcpy

fiberCable = r'C:\Users\broadhurstn\Documents\ArcGIS\Default.gdb\FIBERCABLE'
fibercableToConduit = r'C:\Users\broadhurstn\Documents\ArcGIS\Default.gdb\FibercableToConduit'
conduit = r'C:\Users\broadhurstn\Documents\ArcGIS\Default.gdb\CONDUIT'

# Populate a dictionary that maps segment ID to the placed/planned date
segment_dates = {}
with arcpy.da.SearchCursor(fiberCable, ['segmentid', 'bv_cable_placed_planned']) as cursor:
    for row in cursor:
        segment_dates[row[0]] = row[1]

# Populate a dictionary that maps conduit ID to a two-item list containing segment ID and placed/planned date
ftcs = {}
with arcpy.da.SearchCursor(fibercableToConduit, ['conduitipid', 'segmentid']) as cursor:
    for row in cursor:
        # Use .get() so that it won't fail if a segment ID isn't in the dictionary
        ftcs[row[0]] = [row[1], segment_dates.get(row[1])]

# Populate conduit 'bv_placed_est' field        
with arcpy.da.UpdateCursor(conduit, ['ipid', 'bv_placed_est_1']) as cursor:
    for row in cursor:
        for k, v in ftcs.items():
            if row[0] == k:
                row[1] = str(v[1])
                cursor.updateRow(row)

The above code and feature class table produce a dictionary that fills as follows: {'daac02d0-bf62-4968-b1cd-0fe36c391b2f': ['55a16ddd-2d1d-443b-9935-ecafd2bc7d3b']

How do I add a second value to the dictionary using the below feature class table?

enter image description here

Notice how the value in the dictionary matches "segment ID" in this feature class. I want to have the the SearchCursor look at the current dictionary, see if there is a value that matches the "SegmentID" field values and append the field "Cable Placed Planned" to it's corresponding dictionary item as a second value.

Expected output for the first item:

{'daac02d0-bf62-4968-b1cd-0fe36c391b2f': ['55a16ddd-2d1d-443b-9935-ecafd2bc7d3b', '3/28/2020']

What I've created so far, which doesn't work correctly:

with arcpy.da.SearchCursor(FIBERCABLE, ['segmentid', 'bv_cable_placed_planned']) as cursor:
    for row in cursor:
        if FTCDICT[str(row[0])] == str(row[0]):
            FTCDICT[str(row[1])] = str(row[1])
2

The most efficient way is to create two dictionaries:

  1. Map segment ID to placed/planned date
  2. Map conduit ID to your desired list by using the first dictionary

Here's an example:

import arcpy

tbl_cables = 'FIBERCABLE'
tbl_ftc = 'FTC'

field_segment = 'segmentid'
field_conduit = 'conduitipid'
field_date = 'bv_cable_placed_planned'

# Populate a dictionary that maps segment ID to the placed/planned date
segment_dates = dict()
with arcpy.da.SearchCursor(tbl_cables, [field_segment, field_date]) as cursor:
    for row in cursor:
        segment_dates[row[0]] = row[1]

# Populate a dictionary that maps conduit ID to a two-item list containing segment ID 
# and placed/planned date
ftcs = dict()
with arcpy.da.SearchCursor(tbl_ftc, [field_conduit, field_segment]) as cursor:
    for row in cursor:
        # Use .get() so that it won't fail if a segment ID isn't in the dictionary
        ftcs[row[0]] = [row[1], segment_dates.get(row[1])]
1

You can use collections.defaultdict(list):

from collections import defaultdict
import arcpy

FTC = r'C:\data.gdb\features'
FIBERCABLE = r'C:\data.gdb\cables'

d = defaultdict(list)
with arcpy.da.SearchCursor(FTC, ['conduitipid', 'segmentid']) as cursor:
    for con, seg in cursor:
        d[con].append(seg)

d2 = {k:v for k,v in arcpy.da.SearchCursor(FIBERCABLE,['Segment ID','Cabled Placed Planned'])}

for key, val in d.iteritems(): #d.items() in py3
    if val[0] in d2:
        d[key].append(d2[val[0]])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.