``````

In [5]:

'''
with open("2016_cd_2a_2.txt") as f:
print(lines)

#假如輸出要保留跳行符號
with open(fname) as f:

#假如輸出不要保留跳行符號
with open(fname) as f:

# get list
import csv
with open("2016_cd_2a_2.txt", 'r') as f:
print(your_list)
'''
# readlines() 會以每一行資料當作一個字串, 作為 element, 並輸出一個數列資料
result = []
with open("2016_cd_2a_2.txt", 'r') as f:
#print(content)
#print(len(content))
# 逐 element 處理
for i in range(len(content)):
for line in content[i].splitlines():
#print(content[i].splitlines())
result.append(list(line.split(",")))
# result element 即為各分組的學員學號數列資料
# 這裡要先以遞增排序處理各組的數列
group_sorted = []
for i in range(len(result)):
group_list = sorted(list(filter(None, result[i])))
group_sorted.append(group_list)
print("已經確認各組組長的數列:", group_sorted)
# 利用 sorted(), 對全班各組數列所組成的數列進行遞增排序
final_result = sorted(group_sorted)
print("分組結果:", final_result)
# 取出 final_result 數列中的各組學員學號, 組成一個大數列, 並且用 filter() 去除空字串後, 傳回 iterator 後
# 再用 list() 轉為數列
final_list = list(filter(None, [stud_id for group_list in final_result for stud_id in group_list]))

'''
# 先從 final_result 數列中取出 group_list, 然後再從各 group_list 中取出各學員學號字串, 然後再組成大數列
final_list = [stud_id
for group_list in final_result
for stud_id in group_list]
'''

print(final_list)
# 若採用遞減排續, 即學號數大的組長排前面
#final_result = sorted(result, reverse=True)
group_id = 1
for group_list in final_result:
print("第"+str(group_id)+"組:", group_list)
group_id += 1
#print(sorted(filter(None,result[0])))
number = 0
# 逐組列出分組學號數列
for i in range(len(result)):
# 利用 sorted 以遞增排列, 並用 filter() 移除空字串
print(sorted(filter(None, result[i])))
number += len(sorted(filter(None,result[i])))
# 若採遞減排列
#print(sorted(filter(None,result[i]), reverse=True))
print("共有", number, " 名學生")

``````
``````

['40023139', '40223153', '40323105', '40323106', '40323107', '40323146', '40023234', '40123119', '40123141', '40123149', '40123216', '40123227', '40123255', '40323101', '40323102', '40323103', '40323108', '40323124', '40323109', '40323130', '40323135', '40323136', '40323138', '40323144', '40323110', '40323113', '40323116', '40323121', '40323151', '40323111', '40323117', '40323118', '40323119', '40323120', '40323122', '40323112', '40323133', '40323147', '40323152', '40323155', '40323156', '40323123', '40323131', '40323137', '40323143', '40323145', '40323154', '40323125', '40323126', '40323132', '40323149', '40323150', '40323153', '40323127', '40323128', '40323139', '40323141']

['40323101', '40323102', '40323103', '40323108', '40323124']
['40323109', '40323130', '40323135', '40323136', '40323138', '40323144']
['40323111', '40323117', '40323118', '40323119', '40323120', '40323122']
['40323110', '40323113', '40323116', '40323121', '40323151']
['40323112', '40323133', '40323147', '40323152', '40323155', '40323156']
['40023139', '40223153', '40323105', '40323106', '40323107', '40323146']
['40023234', '40123119', '40123141', '40123149', '40123216', '40123227', '40123255']
['40323125', '40323126', '40323132', '40323149', '40323150', '40323153']
['40323123', '40323131', '40323137', '40323143', '40323145', '40323154']
['40323127', '40323128', '40323139', '40323141']

``````
``````

In [7]:

# readlines() 會以每一行資料當作一個字串, 作為 element, 並輸出一個數列資料
result = []
with open("2016_cd_2a_2.txt", 'r') as f:
#print(content)
#print(len(content))
# 逐 element 處理
for i in range(len(content)):
for line in content[i].splitlines():
#print(content[i].splitlines())
result.append(list(line.split(",")))
# result element 即為各分組的學員學號數列資料
# 這裡要先以遞增排序處理各組的數列
group_sorted = []
for i in range(len(result)):
group_list = sorted(list(filter(None, result[i])))
group_sorted.append(group_list)
print("各組已經過遞增排序, 各組第一位即為組長:", group_sorted)

'''
[['40323101', '40323102', '40323103', '40323108', '40323124'],
['40323109', '40323130', '40323135', '40323136', '40323138', '40323144'],
['40323111', '40323117', '40323118', '40323119', '40323120', '40323122'],
['40323110', '40323113', '40323116', '40323121', '40323151'],
['40323112', '40323133', '40323147', '40323152', '40323155', '40323156'],
['40023139', '40223153', '40323105', '40323106', '40323107', '40323146'],
['40023234', '40123119', '40123141', '40123149', '40123216', '40123227', '40123255'],
['40323125', '40323126', '40323132', '40323149', '40323150', '40323153'],
['40323123', '40323131', '40323137', '40323143', '40323145', '40323154'],
['40323127', '40323128', '40323139', '40323141']]
'''

# 利用 sorted(), 對全班各組數列所組成的數列進行遞增排序
final_result = sorted(group_sorted)
print("分組結果:", final_result)

'''
[['40023139', '40223153', '40323105', '40323106', '40323107', '40323146'],
['40023234', '40123119', '40123141', '40123149', '40123216', '40123227', '40123255'],
['40323101', '40323102', '40323103', '40323108', '40323124'],
['40323109', '40323130', '40323135', '40323136', '40323138', '40323144'],
['40323110', '40323113', '40323116', '40323121', '40323151'],
['40323111', '40323117', '40323118', '40323119', '40323120', '40323122'],
['40323112', '40323133', '40323147', '40323152', '40323155', '40323156'],
['40323123', '40323131', '40323137', '40323143', '40323145', '40323154'],
['40323125', '40323126', '40323132', '40323149', '40323150', '40323153'],
['40323127', '40323128', '40323139', '40323141']]
'''

# 取出 final_result 數列中的各組學員學號, 組成一個大數列, 並且用 filter() 去除空字串後, 傳回 iterator 後
# 再用 list() 轉為 數列
final_list = list(filter(None, [stud_id for group_list in final_result for stud_id in group_list]))

'''
# 先從 final_result 數列中取出 group_list, 然後再從各 group_list 中取出各學員學號字串, 然後再組程大數列
final_list = [stud_id
for group_list in final_result
for stud_id in group_list]
'''

print("課堂上登記共有", len(final_list), " 名學生")

# 讀進學校註冊學員名單, 去除所有跳行符號, 輸出一個大字串
with open("2016_cd_2a_3.txt") as f:
#print(content)
print("選課名單上共有", len(content), " 名學生")
# 檢查兩份資料的差異
# 列出已經選課但是並未在第1週上課出現的名單
no_w1 = [stud_id for stud_id in content if stud_id not in final_list]
print("已選課, 但第1週缺席名單:", no_w1)
# 第1週擬加選的名單
add_w1 = [stud_id for stud_id in final_list if stud_id not in content]

``````
``````

``````
``````

In [10]:

# spring_2a 數列即為執行 2016s_g1_w1_task0.py 之後, 所得到的 final_result 數列
# 這個數列是帶有空白字串的分組學號名單數列的組合數列
'''

['40023234', '40123119', '40123141', '40123149', '40123216', '40123227', '40123255'],
['40323101', '40323102', '40323103', '40323108', '40323124'],
['40323109', '40323130', '40323135', '40323136', '40323138', '40323144'],
['40323110', '40323113', '40323116', '40323121', '40323151'],
['40323111', '40323117', '40323118', '40323119', '40323120', '40323122'],
['40323112', '40323133', '40323147', '40323152', '40323155', '40323156'],
['40323123', '40323131', '40323137', '40323143', '40323145', '40323154'],
['40323125', '40323126', '40323132', '40323149', '40323150', '40323153'],
['40323127', '40323128', '40323139', '40323141']]
'''

#spring_2a =  [['40023139', '40223153', '40323105', '40323106', '40323107', '40323146'], ['40023234', '40123119', '40123141', '40123149', '40123216', '40123227', '40123255'], ['40323101', '40323102', '40323103', '40323108', '40323124'], ['40323109', '40323130', '40323135', '40323136', '40323138', '40323144'], ['40323110', '40323113', '40323116', '40323121', '40323151'], ['40323111', '40323117', '40323118', '40323119', '40323120', '40323122'], ['40323112', '40323133', '40323147', '40323152', '40323155', '40323156'], ['40323123', '40323131', '40323137', '40323143', '40323145', '40323154'], ['40323125', '40323126', '40323132', '40323149', '40323150', '40323153'], ['40323127', '40323128', '40323139', '40323141']]
spring_2a = final_result
# 第 i 組學號數列 為 spring_2a[i-1], i 從 1 到 10 共有 10 組
# 若要先照排依組序排座位, 且空字串由下一組可用學號補上
# 以排為先, 然後列, 共有 9 排 7 列可以排座位

seat_by_column = []
for row in range(7):
for column in range(10):
# 因為各分組數列的長度並不相同, 但是最長的有 7 位組員, 因此若無法取得的資料 (因為索引超值), 就補上空字串
try:
seat_by_column.append(spring_2a[column][row])
except:
seat_by_column.append("")

print("去除空白字串前的座位數列:", seat_by_column)

'''

'''

# 然後利用 filter(None, seat_by_column) 去除空白字串, 就可以得到以 column 為主的座位排序

seat_by_column = list(filter(None, seat_by_column))
print("以排為主的座位數列:", seat_by_column)

'''

'''

# 然後每 7 個取為 1 排, 即可得到以排為主的座位序列

N = 7
column_list = [seat_by_column[n:n+N] for n in range(0, len(seat_by_column), N)]
print("每 7 個組員一排的數列:", column_list)

# 根據 column_list, 建立一個 dictionary, 其中學號為 index, 座位號為對應值
seat_dict = {}
for column in range(len(column_list)):
for i in range(7):
try:
seat_dict.update({column_list[column][i]: (column, i)})
except:
seat_dict.update({"": ""})
# 根據學號, 排序 dictionary 的方法
import operator
seat_dict_sort = sorted(seat_dict.items(), key = operator.itemgetter(0), reverse = False)

# 依照學號順序, 列出座位表
for i in range(len(seat_dict_sort)):
print(seat_dict_sort[i])

'''
# 以排為主, 將 seat_by_column 數列, 以每 7 個一排, 組成座位數列
# (1,1), (1, 2) to (9, 1)

[['40023139', '40023234', '40323101', '40323109', '40323110', '40323111', '40323112'],
['40323123', '40323125', '40323127', '40223153', '40123119', '40323102', '40323130'],
['40323113', '40323117', '40323133', '40323131', '40323126', '40323128', '40323105'],
['40123141', '40323103', '40323135', '40323116', '40323118', '40323147', '40323137'],
['40323132', '40323139', '40323106', '40123149', '40323108', '40323136', '40323121'],
['40323119', '40323152', '40323143', '40323149', '40323141', '40323107', '40123216'],
['40323124', '40323138', '40323151', '40323120', '40323155', '40323145', '40323150'],
['40323146', '40123227', '40323144', '40323122', '40323156', '40323154', '40323153'],
['40123255']]
# 若要轉換為以 row 為主的電腦教室數列, 以方便 html table 處理, 則需要利用 zip 進行 transpose
# (9,1)->(1,1), (8,1)->(1,2) ... (1,1)->(1,9)
'''

# dont know why .reverse() did not work, 只有 [::-1] 可以 reverse list elements
#print(column_list[::-1])

# 因為經由 zip 逐一重新 transpose 的列資料, 必須配合最大 (也就是總共有 7 列) 列數補上空白字串 (也就是空位)
# 所以不能使用 zip, 而必須導入 zip_longest 模組方法

from itertools import zip_longest

# zip list of lists, 特別注意下列 column_list 前方的 *

'''
The reverse situation occurs when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments. For instance, the built-in range() function expects separate start and stop arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:

https://docs.python.org/3/tutorial/controlflow.html#tut-unpacking-arguments
'''

spring_2a_final_seat = list(zip_longest(*column_list[::-1], fillvalue=""))
print(spring_2a_final_seat)

# 最後轉成 html table 標註格式

print("<table border='1'>")
for row in range(len(spring_2a_final_seat)):
print("<tr>")
# 因為每一 row 有 9 個位子
for i in range(9):
print("<td>", spring_2a_final_seat[row][i], "</td>")
print("</tr>")
print("</table>")

'''
# 最後得到的結果, 可以利用 html table 顯示在網頁上, 也就是以列為主的座位名單數列
[('40123255', '40323146', '40323124', '40323119', '40323132', '40123141', '40323113', '40323123', '40023139'),
('', '40123227', '40323138', '40323152', '40323139', '40323103', '40323117', '40323125', '40023234'),
('', '40323144', '40323151', '40323143', '40323106', '40323135', '40323133', '40323127', '40323101'),
('', '40323122', '40323120', '40323149', '40123149', '40323116', '40323131', '40223153', '40323109'),
('', '40323156', '40323155', '40323141', '40323108', '40323118', '40323126', '40123119', '40323110'),
('', '40323154', '40323145', '40323107', '40323136', '40323147', '40323128', '40323102', '40323111'),
('', '40323153', '40323150', '40123216', '40323121', '40323137', '40323105', '40323130', '40323112')]
'''

``````
``````

('', '')
('40023139', (0, 0))
('40023234', (0, 1))
('40123119', (1, 4))
('40123141', (3, 0))
('40123149', (4, 3))
('40123216', (5, 6))
('40123227', (7, 1))
('40123255', (8, 0))
('40223153', (1, 3))
('40323101', (0, 2))
('40323102', (1, 5))
('40323103', (3, 1))
('40323105', (2, 6))
('40323106', (4, 2))
('40323107', (5, 5))
('40323108', (4, 4))
('40323109', (0, 3))
('40323110', (0, 4))
('40323111', (0, 5))
('40323112', (0, 6))
('40323113', (2, 0))
('40323116', (3, 3))
('40323117', (2, 1))
('40323118', (3, 4))
('40323119', (5, 0))
('40323120', (6, 3))
('40323121', (4, 6))
('40323122', (7, 3))
('40323123', (1, 0))
('40323124', (6, 0))
('40323125', (1, 1))
('40323126', (2, 4))
('40323127', (1, 2))
('40323128', (2, 5))
('40323130', (1, 6))
('40323131', (2, 3))
('40323132', (4, 0))
('40323133', (2, 2))
('40323135', (3, 2))
('40323136', (4, 5))
('40323137', (3, 6))
('40323138', (6, 1))
('40323139', (4, 1))
('40323141', (5, 4))
('40323143', (5, 2))
('40323144', (7, 2))
('40323145', (6, 5))
('40323146', (7, 0))
('40323147', (3, 5))
('40323149', (5, 3))
('40323150', (6, 6))
('40323151', (6, 2))
('40323152', (5, 1))
('40323153', (7, 6))
('40323154', (7, 5))
('40323155', (6, 4))
('40323156', (7, 4))
[('40123255', '40323146', '40323124', '40323119', '40323132', '40123141', '40323113', '40323123', '40023139'), ('', '40123227', '40323138', '40323152', '40323139', '40323103', '40323117', '40323125', '40023234'), ('', '40323144', '40323151', '40323143', '40323106', '40323135', '40323133', '40323127', '40323101'), ('', '40323122', '40323120', '40323149', '40123149', '40323116', '40323131', '40223153', '40323109'), ('', '40323156', '40323155', '40323141', '40323108', '40323118', '40323126', '40123119', '40323110'), ('', '40323154', '40323145', '40323107', '40323136', '40323147', '40323128', '40323102', '40323111'), ('', '40323153', '40323150', '40123216', '40323121', '40323137', '40323105', '40323130', '40323112')]
<table border='1'>
<tr>
<td> 40123255 </td>
<td> 40323146 </td>
<td> 40323124 </td>
<td> 40323119 </td>
<td> 40323132 </td>
<td> 40123141 </td>
<td> 40323113 </td>
<td> 40323123 </td>
<td> 40023139 </td>
</tr>
<tr>
<td>  </td>
<td> 40123227 </td>
<td> 40323138 </td>
<td> 40323152 </td>
<td> 40323139 </td>
<td> 40323103 </td>
<td> 40323117 </td>
<td> 40323125 </td>
<td> 40023234 </td>
</tr>
<tr>
<td>  </td>
<td> 40323144 </td>
<td> 40323151 </td>
<td> 40323143 </td>
<td> 40323106 </td>
<td> 40323135 </td>
<td> 40323133 </td>
<td> 40323127 </td>
<td> 40323101 </td>
</tr>
<tr>
<td>  </td>
<td> 40323122 </td>
<td> 40323120 </td>
<td> 40323149 </td>
<td> 40123149 </td>
<td> 40323116 </td>
<td> 40323131 </td>
<td> 40223153 </td>
<td> 40323109 </td>
</tr>
<tr>
<td>  </td>
<td> 40323156 </td>
<td> 40323155 </td>
<td> 40323141 </td>
<td> 40323108 </td>
<td> 40323118 </td>
<td> 40323126 </td>
<td> 40123119 </td>
<td> 40323110 </td>
</tr>
<tr>
<td>  </td>
<td> 40323154 </td>
<td> 40323145 </td>
<td> 40323107 </td>
<td> 40323136 </td>
<td> 40323147 </td>
<td> 40323128 </td>
<td> 40323102 </td>
<td> 40323111 </td>
</tr>
<tr>
<td>  </td>
<td> 40323153 </td>
<td> 40323150 </td>
<td> 40123216 </td>
<td> 40323121 </td>
<td> 40323137 </td>
<td> 40323105 </td>
<td> 40323130 </td>
<td> 40323112 </td>
</tr>
</table>

Out[10]:

"\n# 最後得到的結果, 可以利用 html table 顯示在網頁上, 也就是以列為主的座位名單數列\n[('40123255', '40323146', '40323124', '40323119', '40323132', '40123141', '40323113', '40323123', '40023139'), \n('', '40123227', '40323138', '40323152', '40323139', '40323103', '40323117', '40323125', '40023234'), \n('', '40323144', '40323151', '40323143', '40323106', '40323135', '40323133', '40323127', '40323101'), \n('', '40323122', '40323120', '40323149', '40123149', '40323116', '40323131', '40223153', '40323109'), \n('', '40323156', '40323155', '40323141', '40323108', '40323118', '40323126', '40123119', '40323110'), \n('', '40323154', '40323145', '40323107', '40323136', '40323147', '40323128', '40323102', '40323111'), \n('', '40323153', '40323150', '40123216', '40323121', '40323137', '40323105', '40323130', '40323112')]\n"

``````
``````

In [ ]:

``````