Этот код:
import calendar
import pprint
year = 2011
days_in_week = 8
c = calendar.Calendar()
# First month with zeroes to create full week
l = list(c.itermonthdays(year, 1))
# Slice by days_in_week
l2 = [[l[a*days_in_week:a*days_in_week+days_in_week] for a in range(len(l) / days_in_week + 1)]]
# Add zeroes if needed and slice rest
l2[-1][-1] += [0] * (days_in_week - (len(l2[-1][-1])))
if l2[-1][-1].count(0) == days_in_week:
l2[-1] = l2[-1][:-1]
for month in range(2, 13):
# Days in month
l = range(1, calendar.monthrange(year, month)[1]+1)
# Add needed zeroes to the beginning
zeroes_at_end = l2[-1][-1].count(0)
l = [0] * ((days_in_week - zeroes_at_end) % days_in_week) + l
# Slice by days_in_week
l2 += [[l[a*days_in_week:a*days_in_week+days_in_week] for a in range(len(l) / days_in_week + 1)]]
# Add zeroes if needed and slice rest
l2[-1][-1] += [0] * (days_in_week - (len(l2[-1][-1])))
if l2[-1][-1].count(0) == days_in_week:
l2[-1] = l2[-1][:-1]
pprint.pprint(l2)
Дает этот результат:
[[[0, 0, 0, 0, 0, 1, 2, 3],
[4, 5, 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27],
[28, 29, 30, 31, 0, 0, 0, 0]],
[[0, 0, 0, 0, 1, 2, 3, 4],
[5, 6, 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27, 28]],
[[1, 2, 3, 4, 5, 6, 7, 8],
[9, 10, 11, 12, 13, 14, 15, 16],
[17, 18, 19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30, 31, 0]],
[[0, 0, 0, 0, 0, 0, 0, 1],
[2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23, 24, 25],
[26, 27, 28, 29, 30, 0, 0, 0]],
[[0, 0, 0, 0, 0, 1, 2, 3],
[4, 5, 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27],
[28, 29, 30, 31, 0, 0, 0, 0]],
[[0, 0, 0, 0, 1, 2, 3, 4],
[5, 6, 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27, 28],
[29, 30, 0, 0, 0, 0, 0, 0]],
[[0, 0, 1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12, 13, 14],
[15, 16, 17, 18, 19, 20, 21, 22],
[23, 24, 25, 26, 27, 28, 29, 30],
[31, 0, 0, 0, 0, 0, 0, 0]],
[[0, 1, 2, 3, 4, 5, 6, 7],
[8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30, 31]],
[[1, 2, 3, 4, 5, 6, 7, 8],
[9, 10, 11, 12, 13, 14, 15, 16],
[17, 18, 19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30, 0, 0]],
[[0, 0, 0, 0, 0, 0, 1, 2],
[3, 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24, 25, 26],
[27, 28, 29, 30, 31, 0, 0, 0]],
[[0, 0, 0, 0, 0, 1, 2, 3],
[4, 5, 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27],
[28, 29, 30, 0, 0, 0, 0, 0]],
[[0, 0, 0, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 10, 11, 12, 13],
[14, 15, 16, 17, 18, 19, 20, 21],
[22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 0, 0, 0, 0, 0, 0]]]
С вами все будет в порядке.