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返回沒有?
修正雛鳥指出的錯誤後,函數'sem1Sort1'和'sem1Sort2'將完全相同,那麼爲什麼兩個不同的函數呢?而且,'list'在python中是一個_type_,所以創建一個名爲'list'的變量並不那麼明智。 – 0605002
夥計們,你不需要投票給我......我只是一個尋找答案的新手。抱歉。 – LucasGrillos
回答後請不要添加更多問題。 – 0605002