0

I have dictionary called nested_group_map based on 4 groups each with a key called shed_count in each group. How could I make a program that will add +1 to each shed_count in a range loop fashion by iterating through the nested_group_map keys/values and then break when the number of cycles through the range loop is complete? Basically what I am thinking is the shed_count in each group should == cycles which 12.

Sorry sort of an odd question, hopefully this makes sense.

import numpy as np

cycles = 12

nested_group_map = {
    'group_l1n' : {
    'shed_count': 0,
    },
    'group_l1s' : {
    'shed_count': 0,
    },
    'group_l2n' : {
    'shed_count': 0,
    },
    'group_l2s' : {
    'shed_count': 0,
    }
}


def cycle_checker(cycles):
    check_sum = [int(cycles)]
    print(f'cycle_checker after for loop sum is {sum(check_sum)}')
    print(f'cycle_checker appended after for loop is {check_sum}')

    for group in nested_group_map:
        for k,v in nested_group_map[group].items():
            if k in ('shed_count'):
                check_sum.append(int(v))
                
    print(f'cycle_checker after for loop sum is {sum(check_sum)}')
    print(f'cycle_checker appended after for loop is {check_sum}')

    return np.all(check_sum)


while not cycle_checker(cycles):
    for group in nested_group_map:
        for v in nested_group_map[group].values():
            v = v + 1
            print(v)

This just creates an infinite loop, any newbie tips appreciated : )

bbartling
  • 3,288
  • 9
  • 43
  • 88
  • Sorry, I don't understand the problem at all. What is the logical/mathematical rule that tells you how many `cycles` there should be? What is a "cycle" for you, and why is `shed_count` called that? What are the values in the `check_sum` list supposed to represent, and what is the logical/mathematical rule that you want to apply to that list? And why are you using `np.all` on a plain Python list? – Karl Knechtel Jul 31 '21 at 19:41

2 Answers2

0

When you do

for v in nested_group_map[group].values():
    v = v + 1

you aren't changing nested_group_map in any way. Take a look at this answer for a brief explanation. For this, you have to explicity write to it using a key:

group_map = nested_group_map[group]
for k, v in group_map.items():
    group_map[k] = v + 1
enzo
  • 9,861
  • 3
  • 15
  • 38
0

This is the complete answer with the help of @enzo. I also had to remove the np.all() for what ever reason I couldnt get that too work. So I used return sum(check_sum) == len(check_sum) * cycles instead in the cycle_checker function that returns boolean

cycles = 12

# data structure
nested_group_map = {
    'group_l1n' : {
    'shed_count': 0,
    },
    'group_l1s' : {
    'shed_count': 0,
    },
    'group_l2n' : {
    'shed_count': 0,
    },
    'group_l2s' : {
    'shed_count': 0,
    }
}


def cycle_checker(cycles):
    check_sum = [int(cycles)]
    #print(f'cycle_checker after for loop sum is {sum(check_sum)}')
    #print(f'cycle_checker appended after for loop is {check_sum}')

    for group in nested_group_map:
        for k,v in nested_group_map[group].items():
            if k in ('shed_count'):
                check_sum.append(int(v))
        
    #print(f'cycle_checker after for loop sum is {sum(check_sum)}')
    print(f'cycle_checker appended after for loop is {check_sum}')
    
    # returns boolean
    return sum(check_sum) == len(check_sum) * cycles


def run_another_cylce():
    for group in nested_group_map:
        group_map = nested_group_map[group]
        for k, v in group_map.items():
            group_map[k] = v + 1
            
            
while not cycle_checker(cycles):
    run_another_cylce()

This is what I was hoping to see, basically fill the bins (shed_count) until they are ALL filled up to cycles

This prints:

cycle_checker appended after for loop is [12, 0, 0, 0, 0]
cycle_checker appended after for loop is [12, 1, 1, 1, 1]
cycle_checker appended after for loop is [12, 2, 2, 2, 2]
cycle_checker appended after for loop is [12, 3, 3, 3, 3]
cycle_checker appended after for loop is [12, 4, 4, 4, 4]
cycle_checker appended after for loop is [12, 5, 5, 5, 5]
cycle_checker appended after for loop is [12, 6, 6, 6, 6]
cycle_checker appended after for loop is [12, 7, 7, 7, 7]
cycle_checker appended after for loop is [12, 8, 8, 8, 8]
cycle_checker appended after for loop is [12, 9, 9, 9, 9]
cycle_checker appended after for loop is [12, 10, 10, 10, 10]
cycle_checker appended after for loop is [12, 11, 11, 11, 11]
cycle_checker appended after for loop is [12, 12, 12, 12, 12]
bbartling
  • 3,288
  • 9
  • 43
  • 88