# Create dictionary with segmentid as key, calculated length as value1, measured length as value2 fiberCableLengths = defaultdict(list) with arcpy.da.SearchCursor(fiberCable, ['segmentid', 'calculatedlength', 'measuredlength']) as cursor: for row in cursor: fiberCableLengths[row[0]].append(row[1]) fiberCableLengths[row[0]].append(row[2]) # Create slackloop dictionary with segmentidfkey as key, total up the length values of each slackloop under that segmentidfkey, and store as the dictionary value slackloopLengths = defaultdict(list) with arcpy.da.SearchCursor(slackLoop, ['segmentidfkey', 'length']) as cursor: for row in cursor: if row[1] is not None: slackloopLengths[row[0]].append(int(row[1])) sumValueDict = {k: sum(v) for k, v in slackloopLengths.items()} # For each fibercable segmentid in dictionary, If calculated length + total slackloop length = measured length – Add to good list, Else – Add to bad list goodList = [] badList = [] with arcpy.da.SearchCursor(fiberCable, ['segmentid', 'calculatedlength', 'measuredlength']) as cursor: for row in cursor: for k, v in sumValueDict.items(): if row[0] == k: if row[1] and row[2] is not None: if int(row[1]) + int(v) == int(row[2]): goodList.append(str(row[0])) print "Good List: " + row[0] print int(row[1]) + int(v) print int(row[2]) else: badList.append(str(row[0])) print "Bad List: " + row[0] print int(row[1]) + int(v) print int(row[2]) print "Good List Size: " + str(len(goodList)) print "Bad List Size: " + str(len(badList))
The code above creates a dictionary where each item looks like the following:
'1f169eae-5d7c-4ebb-90cd-abbe0e1b72dd': [50.0, 50.0, 50.0, 50.0]
How do I get it to sum the values for each key? The output I'd be looking for here would be:
4ebb-90cd-abbe0e1b72dd': [200]
Resources I've looked at:
https://stackoverflow.com/questions/4880960/how-to-sum-all-the-values-in-a-dictionary/4881100 https://stackoverflow.com/questions/34849651/how-to-add-the-values-of-dictionary-with-same-keys-in-python https://stackoverflow.com/questions/34849651/how-to-add-the-values-of-dictionary-with-same-keys-in-python