我有一周的提交字典。我希望以每周日历样式的列打印出来。
{
'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']}
似乎使用numpy
是在列中打印出来的方法。通过添加一些虚拟字符串,并将字典转换为数组数组,我也能够“均匀”列表。我正在努力解决的问题是如何将数组数组转换为正确的numpy
数组。
“Evened out”阵列数组:
[
['Commit: 09:40AM remove transform and tweak span placement in hamburger\n', 'X', 'X', 'X', 'X']
['Commit: 02:19PM Change blockquote font and width\n', 'X', 'X', 'X', 'X']
['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 yeti to footer\n', 'Commit: 03:05PM Merge from p-redesign\n']
['Commit: 10:25AM Design qa fixes on /clients page\n', 'X', 'X', 'X', 'X']
]
我尝试了什么:
nump_array = numpy.array(array_of_arrays)
print(nump_array[:,0])
总是错误IndexError: too many indices for array
。我想我需要做的是进入并将这些内部数组转换为numpy数组然后vstack
它们,但我还不清楚如何处理numpy。我也想知道我是否应该从一开始就这么快就把字典丢弃。
以下是我正在寻找的缩短版本:
| Mon | Tue | Wed | Thu | Fri |
| 04:15 | 09:40 | 10:32| 04:12 | 11:00 |
| Move..|Do a ..|Add .. | Use ..| Change|
| 03:52 | | | | |
答案 0 :(得分:2)
我认为你可以在没有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
obviates the need to "even out" your arrays ...它只返回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