1
# 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

1
  • What is your next step? You could use summary statistics Commented Jun 24, 2019 at 15:56

1 Answer 1

8

Iterate over the dict items and construct a new dict from the key and sum of the values:

>>> slackloopLength = {'abc': [1,2,4,7], 'def': [1,3,5,7,9]} >>> {k: sum(v) for k, v in slackloopLength.items()} {'abc': 14, 'def': 25} >>> 

The way to read this construction is:

We're going to make a dict:

 { 

and each element will have key k and value sum(v):

 {k: sum(v) 

where k and v are each of the key-value pairs of elements of slackloopLength:

 {k: sum(v) for k, v in slackloopLength.items()} 
4
  • Would you mind taking that out of list comprehension format? I've yet to wrap my mind around that format. Commented Jun 24, 2019 at 15:23
  • 1
    How else would you like it? That's probably the most "pythonic" way of doing this and wrapping it in a "for" loop would be inefficient and bad style. I've spelt it out now in the way I read these things. Commented Jun 24, 2019 at 15:52
  • The code above creates a temporary output that sums the values of the original slackloopLengths dictionary container? How would I get it to update the values in my original dictionary? Commented Jun 24, 2019 at 16:30
  • 2
    Reassign it: slackloopLength = {blah blah} Commented Jun 24, 2019 at 17:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.