[英]Split each list of lists into sublists of given lengths
我想將每個列表列表拆分為給定長度的子列表。 我有一個看起來像這樣的課程數組:
[['CS105', 'ENG101', 'MATH101', 'GER', 'ENG102', 'CS230', 'MATH120', 'GER', 'CS205', 'FREE', 'GER', 'CS106', 'CS215', 'CS107', 'ENG204', 'GER', 'MATH220', 'CS300', 'CS206', 'CS306', 'GER', 'FREE', 'CS312', 'CS450', 'GER', 'CS321', 'FREE', 'CS325', 'GER', 'CS322', 'MAJOR', 'CS310', 'STAT205', '', 'CS443', 'CS412', 'CS421', 'GER', 'CS444', 'FREE', 'FREE','','',''], ['CS105', 'ENG101', 'MATH101', 'GER', 'ENG102', 'CS230', 'MATH120', 'GER', 'CS205', 'FREE', 'GER', 'CS106', 'CS215', 'CS107', 'ENG204', 'GER', 'MATH220', 'CS300', 'CS206', 'CS306', 'GER', 'FREE', 'CS312', 'CS450', 'GER', 'CS321', 'FREE', 'CS325', 'GER', 'CS322', 'MAJOR', 'CS310', 'STAT205', '', 'CS443', 'CS412', 'CS421', 'GER', 'CS444', 'FREE', 'FREE','','',''],...]
我想將每個列表拆分為子列表,它們看起來像這樣:
[[['CS105', 'ENG101', 'MATH101', 'GER'],['ENG102', 'CS230', 'MATH120', 'GER'], ['CS205', 'FREE'], ['GER'], ['CS106', 'CS215', 'CS107','ENG204', 'GER'], ['MATH220', 'CS300', 'CS206', 'CS306'], ['GER', 'FREE'], ['CS312'], ['CS450', 'GER', 'CS321', 'FREE', 'CS325'], ['GER', 'CS322', 'MAJOR', 'CS310'], ['STAT205',''], [''], ['CS443', 'CS412', 'CS421', 'GER',''], ['CS444', 'FREE', 'FREE',''],['','']]...]
我到目前為止所做的如下:
schedule = [4, 4, 2, 1, 5, 4, 2, 1, 5, 4, 2, 1, 5, 4, 2, 1]
for i in courses:
Output = [courses[x - y: x] for x, y in zip(accumulate(schedule), schedule)]
print(Output[0])
但是用 Output[0] 打印的是連續 4 個列表,所以據我所知,它可能需要 4 對。 schedule是我希望拆分每個列表的給定長度。 我無法理解我可能需要如何循環才能達到我需要的結果。
為您准備了一段工作代碼:
new_list = []
for c in courses:
_sum = 0
_list = []
for t in schedule:
el = c[_sum:_sum + t]
_sum += t
_list.append(el)
new_list.append(_list)
可能可以更快地完成,但這應該可以完成工作!
如果您有任何問題或無法正常工作,請給我留言。 希望我能幫上忙。
這是一個 function 可以滿足您的要求:
from itertools import accumulate
def divide_into_schedule(input_list, schedule):
if schedule[0] != 0:
schedule.insert(0, 0)
indices = list(accumulate(schedule))
output_list = [list() for _ in input_list]
for start_index, end_index in zip(indices, indices[1:]):
for input_sublist, output_sublist in zip(input_list, output_list):
output_sublist.append(input_sublist[start_index:end_index])
return output_list
這里的核心思想是:
schedule
列表創建要從每個子列表中獲取的索引列表;indices[1:]
),以便這些值成為每個子列表中 select 的開始和結束索引;input_list
和output_list
中的每個子列表上;input_list
“復制粘貼”到output_list
上。通過傳遞輸入列表和調度來調用它:
input_list = [['CS105', 'ENG101', 'MATH101', 'GER', 'ENG102', 'CS230', 'MATH120', 'GER', 'CS205', 'FREE', 'GER', 'CS106', 'CS215', 'CS107', 'ENG204', 'GER', 'MATH220', 'CS300', 'CS206', 'CS306', 'GER', 'FREE', 'CS312', 'CS450', 'GER', 'CS321', 'FREE', 'CS325', 'GER', 'CS322', 'MAJOR', 'CS310', 'STAT205', '', 'CS443', 'CS412', 'CS421', 'GER', 'CS444', 'FREE', 'FREE','','',''], ['CS105', 'ENG101', 'MATH101', 'GER', 'ENG102', 'CS230', 'MATH120', 'GER', 'CS205', 'FREE', 'GER', 'CS106', 'CS215', 'CS107', 'ENG204', 'GER', 'MATH220', 'CS300', 'CS206', 'CS306', 'GER', 'FREE', 'CS312', 'CS450', 'GER', 'CS321', 'FREE', 'CS325', 'GER', 'CS322', 'MAJOR', 'CS310', 'STAT205', '', 'CS443', 'CS412', 'CS421', 'GER', 'CS444', 'FREE', 'FREE','','',''],...]
schedule = [4, 4, 2, 1, 5, 4, 2, 1, 5, 4, 2, 1, 5, 4, 2, 1]
output_list = divide_into_schedule(input_list, schedule)
由於我在評論中提到的拼寫錯誤,結果與您想要的 output 不完全相同,但我相信它可以滿足您的需求。
您最初使用itertools.accumulate
的想法實際上非常好,您只是犯了一些小錯誤:
output = [ [sublist[x - y: x] for x, y in zip(accumulate(schedule), schedule)] for sublist in courses ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.