-1

enter image description hereI have been trying to write a code that if a specific value is in a triple nested dictionary (most inner side) or not however I keep having string indices must be integers even though It is a dictionary.

dictionary is like:

{'Mo': {'10.00': 'COMP100'}, 
    'Tu': ' ', 
    'We': {'12.00': 'UNIV199'}, 
    'Th': ' ', 
    'Fr': ' '}

and I am trying a class code which is not in the dictionary so It should warn the user. like "ECON201"

drokso
  • 1
  • 2
  • Please give the code that triggered the error – mozway Jan 11 '22 at 19:21
  • IMO the inner values for empty days should be `'Tu': {}` instead of strings for type consistency – Cory Kramer Jan 11 '22 at 19:25
  • Mixing types in data structure always makes things difficult. If you used empty dicts instead of single-space strings things would make a lot more sense. Then you could simply write: `any('ECON003' in d_inner.values() for d_inner in d.values())` – Mark Jan 11 '22 at 19:25
  • This is similar to: https://stackoverflow.com/questions/22162321/search-for-a-value-in-a-nested-dictionary-python. You can use one of those solutions and check for when a value is not returned to find your missing classes – oh_my_lawdy Jan 11 '22 at 19:26
  • [Please do not upload images of code/data/errors when asking a question.](http://meta.stackoverflow.com/q/285551) – martineau Jan 11 '22 at 19:38
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jan 19 '22 at 22:03

1 Answers1

0

For type consistency, I recommend using an empty dictionary {} instead of ' ' for empty days. With that in mind, suppose we have the following:

sched = {'Mo': {'10.00': 'COMP100'}, 
    'Tu': {}, 
    'We': {'12.00': 'UNIV199'}, 
    'Th': {}, 
    'Fr': {}}

# code = "COMP100"
code = "ECON201"

Then, we could search for the code as follows:

found = False
for d in sched.values():
    for s in d.values():
        if s == code:
            found = True
            break
    if found:
        break
if found:
    print('code found in schedule')
else:
    print('code not found in schedule')

Alternatively, you can avoid dealing with breaks and storing booleans if you define a function. For instance,

def is_in(sch,c):
    for d in sch.values():
        for s in d.values():
            if s == c:
                return True
    return False

if is_in(sched,code):
    print('code found in schedule')
else:
    print('code not found in schedule') 

Alternatively, here's a recursive version of the function that works for arbitrarily many layers of nesting.

def is_in(sch,c):
    for v in sch.values():
        if isinstance(v,dict):
            if is_in(v,c):
                return True
        elif v == c:
            return True
    return False

if is_in(sched,code):
    print('code found in schedule')
else:
    print('code not found in schedule')

On the other hand, from your image, it seems that you're looking for a way to remove a course from a schedule. To that end, consider the following.

sched = {'Mo': {'10.00': 'COMP100'}, 
    'Tu': {}, 
    'We': {'12.00': 'UNIV199'}, 
    'Th': {}, 
    'Fr': {}}

# code = "COMP100"
code = "ECON201"

# print(sched)

def rem(c,sch):
    for k in sch.copy():
        if isinstance(sch[k],dict):
            rem(c,sch[k])
        else:
            if sch[k]==c:
                del sch[k]
    
rem(code,sched)
print(sched)

In the case that the course does not appear, nothing happens. If the course is found, then all instances of that course are deleted.

Ben Grossmann
  • 4,387
  • 1
  • 12
  • 16