我希望能够采取如下序列:
my_sequence = ['foo', 'bar', 'baz', 'spam', 'eggs', 'cheese', 'yogurt']
使用如下函数:
my_paginated_sequence = get_rows(my_sequence, 3)
要得到:
[['foo', 'bar', 'baz'], ['spam', 'eggs', 'cheese'], ['yogurt']]
这就是我通过思考得出的结论:
def get_rows(sequence, num):
count = 1
rows = list()
cols = list()
for item in sequence:
if count == num:
cols.append(item)
rows.append(cols)
cols = list()
count = 1
else:
cols.append(item)
count += 1
if count > 0:
rows.append(cols)
return rows