Python追加方法无法正常工作

时间:2014-04-28 05:25:33

标签: python arrays list methods dictionary

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()

当我运行此代码时,它打印出SEM1period1罚款,如[“e”,“f”,“g”,“h”],但第二种方法sem1Sort2,似乎没有将任何东西保存到SEM1period2中 - 作为打印声明打印出[]

更新:

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()

为什么打印SEM1period3没有返回?

3 个答案:

答案 0 :(得分:3)

您要在semester1函数中将semester1与整数进行比较,dictsem1Sort2对象,

    for period in semester1:
        if semester1 == 2:

实际上你必须比较整数和dict这样的键,

    for period in semester1:
        if period == 2:

你剩下的就是这样,

    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()

输出:

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

答案 1 :(得分:2)

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()

答案 2 :(得分:1)

为什么这么复杂?

你不需要循环遍历所有dict条目并选择匹配的条目 - 这使得dict对dict的使用毫无意义。

相反,你可以告诉dict给你与给定键相关的值。您可以在main()函数中执行此操作,将函数减少为

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

您可以在main()函数中调用,例如

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()

你将实现你想要的目标。

你甚至可以改进它:

def semSort(semester, selection):

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

    return result

该集使查找更快。

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

更紧凑。