Question

list1 = []
list2 = []
list3 = []

elecStorage = []

elective = ["i", "j", "k", "l"]
prereq = ["a", "b", "c", "d", "e", "f", "g", "h"]

dict1 = {
1: SEM1period1, 2: SEM1period2,
3: SEM1period3, 4: SEM1period3,
5: SEM1period5, 6: SEM1period6,
7: SEM1period7, 8: SEM1period8
}

someNumber = 1


for key, dlist in dict1.items():
    if not dlist:
        list1.append("Free Period")
        someNumber += 1
        continue
    for item in dlist:
        if item in list1:
            continue
        elif item in elective:
            elecStorage.append(item)
        elif item in prereq:
            list1.append(item)
            someNumber += 1
            break
        if someNumber > len(list1):
            for index in elecStorage:
                if index in list1:
                    continue
                else:
                    list1.append(index)
                    someNumber += 1
            break

Note: Currently only SEM1period1, SEM1period2, and SEM1period3 contain anything. SEM1period1 contains

["a", "c", e", "g"]

SEM1period2 contains

["b", "d", "f", "h"]

and SEM1period3 contains

["i", "j", "k", "l"]

When this code runs through it SHOULD save

["a", "b", "i", "Free Period", "Free Period", "Free Period", "Free Period", "Free Period"]

into list1. Instead it saves

["a", "b", "i", "j", "k", "l", "Free Period", "Free Period", "Free Period", "Free Period", "Free Period"]

Why is this? Is there a way I can fix this?

Was it helpful?

Solution

It looks like your problem is a typo:

dict1 = {
1: SEM1period1, 2: SEM1period2,
3: SEM1period3, 4: SEM1period3,
                         #   ^
                         # should be 4!

also: while your code appears to run properly (as of Python 3.3.4), in general I would not trust the order of items in a dictionary! You are assuming that dict1.items() returns period 1, period 2, period 3... but in general a dictionary will return items in arbitrary (ie essentially random) order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top