我认为你可以解决这个问题,无需numpy
并且仅使用 stdlib 模块!
from itertools import zip_longest
d = {'Fri': ['Commit: 04:15PM Move flex to mixin and do mobile-first queries\n',
'Commit: 03:52PM use padding to get the margins\n',
'Commit: 10:09AM Remove field_prepared_logo height\n',
'Commit: 03:15PM Add the final div to footer\n',
'Commit: 03:05PM Merge from redesign\n'],
'Thu': ['Commit: 10:25AM Design qa fixes on /clients page\n'],
'Tue': ['Commit: 09:40AM remove transform and tweak span placement in '
'hamburger\n'],
'Wed': ['Commit: 02:19PM Change blockquote font and width\n']}
for row in zip_longest(d['Tue'], d['Wed'], d['Thu'], d['Fri']):
print(row)
# ('Commit: 09:40AM remove transform and tweak span placement in hamburger\n', 'Commit: 02:19PM Change blockquote font and width\n', 'Commit: 10:25AM Design qa fixes on /clients page\n', 'Commit: 04:15PM Move flex to mixin and do mobile-first queries\n')
# (None, None, None, 'Commit: 03:52PM use padding to get the margins\n')
# (None, None, None, 'Commit: 10:09AM Remove field_prepared_logo height\n')
# (None, None, None, 'Commit: 03:15PM Add the final div to footer\n')
# (None, None, None, 'Commit: 03:05PM Merge from redesign\n')
zip_longest消除了“平衡”阵列的需要...它只是返回None
没有什么可放的地方。你也可以通过fillvalue=''
或类似设置默认值。
您还可以使用有序字典来避免像我一样手动指定日期的顺序。
现在您已经有了单独的行,剩下的就是漂亮打印的练习了。这textwrap module可能是你在这里的朋友。
编辑:这需要一些工作,但这里也处理了漂亮的印刷
maxwidth = (80//len(d)) - 1 # set this to whatever value you want
wrapper = textwrap.TextWrapper(width=maxwidth, subsequent_indent=' ')
wrapped_commits = {k: [wrapper.wrap(commit) for commit in v] for k, v in d.items()}
justified_commits = {k: [line.ljust(maxwidth) for commit in v for line in commit] for k, v in wrapped_commits.items()}
for l in zip_longest(justified_commits['Tue'], justified_commits['Wed'], justified_commits['Thu'], justified_commits['Fri'], fillvalue=' '*maxwidth):
print(' '.join(l))
这是输出:
Commit: 09:40AM Commit: 02:19PM Commit: 10:25AM Commit: 04:15PM
remove transform Change blockquote Design qa fixes on Move flex to mixin
and tweak span font and width /clients page and do mobile-
placement in first queries
hamburger Commit: 03:52PM use
padding to get the
margins
Commit: 10:09AM
Remove field_prepa
red_logo height
Commit: 03:15PM Add
the final div to
footer
Commit: 03:05PM
Merge from
redesign