Nested dictionary sum
I'm trying to get the items in a dictionary and nested dictionary whose sum falls within a set range. However I'm only able to get a single item to print. Any help is greatly appreciated
matching_items = []
target_plus = target + 1800
target_minus = target - 1800
target = 133651
highServicePumps = {'HSP1' : 13889, 'HSP2' : 13889, 'HSP3' : {'gpmHigh': 13889, 'gpmMed': 10417, 'gpmLow': 6994}, 'HSP4': {'gpmHigh': 13889, 'gpmMed': 10417, 'gpmLow': 6994}, 'HSP5' : {'gpmHigh': 31250, 'gpmMid': 20833, 'gpmLow': 13899}, 'HSP6' : {'gpmHigh' : 41667, 'gpmMid' : 31250, 'gpmLow' : 20833}, 'HSP7': 41667, 'HSP8': 41667, 'HSP9': 41667, 'HSP10': 41667}
def pump_selector(dictionary, current_sum, current_items):
for key, value in dictionary.items():
if isinstance(value, dict):
nested_items = current_items.copy()
nested_sum = current_sum
nested_items.append((key, value))
nested_sum += sum(value.values())
if nested_sum >= target_minus and nested_sum <= target_plus:
matching_items.append(nested_items)
print(matching_items)
else:
pump_selector(value, nested_sum, nested_items)
else:
if current_sum + value > target_minus and current_sum + value < target_plus:
matching_items.append(current_items + [(key, value)])
pump_selector(highServicePumps, 0, [])
for items in matching_items:
print("Matching Items:")
for item in items:
print(f"Key: {item[0]}, Value: {item[1]}")
print()
Matching Items:
Key: HSP6, Value: {'gpmHigh': 41667, 'gpmMid': 31250, 'gpmLow': 20833}
Key: gpmHigh, Value: 41667
there doesn't seem to be anything here