2014-04-28 54 views
-4
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 - print語句打印出[]Python的附加方法不能正常工作

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

爲什麼打印SEM1period3返回沒有?

+1

修正雛鳥指出的錯誤後,函數'sem1Sort1'和'sem1Sort2'將完全相同,那麼爲什麼兩個不同的函數呢?而且,'list'在python中是一個_type_,所以創建一個名爲'list'的變量並不那麼明智。 – 0605002

+0

夥計們,你不需要投票給我......我只是一個尋找答案的新手。抱歉。 – LucasGrillos

+1

回答後請不要添加更多問題。 – 0605002

回答

3

這裏您比較semester1整數但semester1dict對象sem1Sort2功能,

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'] 
+0

現在既不返回任何東西......? – LucasGrillos

+0

對不起,我是一個新手,你必須給我更多的方向,對不起。 – LucasGrillos

+0

@LucasGrillos請參閱更新的答案。 – fledgling

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

你寫的像(如果學期== 2:)這需要總字典值,所以我們要提及特定的鍵值 – karnaf

+0

爲了使這是一個很好的答案,你可以指出它們之間的差異和詳細說明。 – glglgl

1

爲什麼這麼複雜?

你不需要遍歷所有的字典條目,並挑選出匹配的條目 - 這使得字典的用法無意義。

相反,你可以告訴字典給你與給定鍵相關聯的值。您可以在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] 

更加緊湊。