Question

def sem1Sort1(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if period == 1:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

    return list

def sem1Sort2(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if semester1 == 2:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

    return list

def main():

    selectionSEM1 = ["a", "b", "c", "d", "e", "f", "g", "h"]
    selectionSEM2 = []

    semester1 = {

    1: ["e", "f", "g", "h"], 2: ["a", "b", "c", "d"] ,

    }

    SEM1period1 = sem1Sort1(semester1, selectionSEM1)
    SEM1period2 = sem1Sort2(semester1, selectionSEM1)

    print SEM1period1
    print SEM1period2

main()

When I run this code it prints out SEM1period1 fine, as ["e", "f", "g", "h"], but the second method sem1Sort2, seems not to save anything into SEM1period2 - as the print statement prints out []

UPDATE:

def sem1Sort1(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if period == 1:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

    return list

def sem1Sort2(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if period == 2:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

    return list

def sem1Sort3(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if period == 3:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

def main():

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


    semester1 = {
    1: ["e", "f", "g", "h"], 2: ["a", "b", "c", "d"] ,
    3: ["i", "j", "k", "l"]
    }

    SEM1period1 = sem1Sort1(semester1, selectionSEM1)
    SEM1period2 = sem1Sort2(semester1, selectionSEM1)
    SEM1period3 = sem1Sort3(semester1, selectionSEM1)

    print SEM1period1
    print SEM1period2
    print SEM1period3

main()

Why does print SEM1period3 return none?

Was it helpful?

Solution

Here you are comparing semester1 with integer but semester1 is dict object in sem1Sort2 function,

    for period in semester1:
        if semester1 == 2:

Actually you have to compare integer with key of dict like this ,

    for period in semester1:
        if period == 2:

And your rest of will be like this ,

    def sem1Sort1(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if period == 1:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

    return list

def sem1Sort2(semester1, selectionSEM1):

    list = []

    for period in semester1:
        if period == 2:
            for index in semester1[period]:
                if index in selectionSEM1:
                    list.append(index)

    return list

def main():

    selectionSEM1 = ["a", "b", "c", "d", "e", "f", "g", "h"]
    selectionSEM2 = []

    semester1 = {

    1: ["e", "f", "g", "h"], 2: ["a", "b", "c", "d"] ,

    }

    SEM1period1 = sem1Sort1(semester1, selectionSEM1)
    SEM1period2 = sem1Sort2(semester1, selectionSEM1)

    print SEM1period1
    print SEM1period2

main()

Output:

['e', 'f', 'g', 'h']
['a', 'b', 'c', 'd']

OTHER TIPS

Why so complicated?

You don't need to loop over all dict entries and pick out the matching one - this makes the whoole usage of a dict pointless.

Instead, you can tell the dict to give you the value associated with the given key. You can do so in the main() function, reducing the function to

def semSort(semester, selection):
    list = []
    for index in semester:
        if index in selection:
            list.append(index)
    return list

which you can call in the main() function such as

def main():
    selectionSEM1 = ["a", "b", "c", "d", "e", "f", "g", "h"]
    selectionSEM2 = []
    semester1 = {
        1: ["e", "f", "g", "h"], 2: ["a", "b", "c", "d"] ,
    }

    SEM1period1 = semSort(semester1[1], selectionSEM1)
    SEM1period2 = semSort(semester1[2], selectionSEM1)

    print SEM1period1
    print SEM1period2

main()

and you'll achieve what you want.

You can even refine it:

def semSort(semester, selection):

    result = []
    sel_set = set(selection)    
    for index in semester:
        if index in sel_set:
            result.append(index)

    return result

The set makes the lookup faster.

def semSort(semester, selection):
    sel_set = set(selection)
    return [index for index in semester if index in sel_set]

is even more compact.

def sem1Sort1(semester1, selectionSEM1):
 list = []
 for period in semester1:
   if period == 1:
     for index in semester1[period]:
       if index in selectionSEM1:
          list.append(index)

 return list

def sem1Sort2(semester1, selectionSEM1):
 list = []
 for period in semester1:
  if period == 2:
   for index in semester1[period]:
    if index in selectionSEM1:
       list.append(index)
 return list

def main():
 selectionSEM1 = ["a", "b", "c", "d", "e", "f", "g", "h"]
 selectionSEM2 = []
 semester1 = {
 1: ["e", "f", "g", "h"], 2: ["a", "b", "c", "d"] ,
 }
 SEM1period1 = sem1Sort1(semester1, selectionSEM1)
 SEM1period2 = sem1Sort2(semester1, selectionSEM1)
 print SEM1period1
 print SEM1period2
main()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top