从python字典打印列 [英] print columns from python dictionary
问题描述
我有一个超过一周的提交字典.我想以每周日历样式的列打印出来.
I have a dictionary of commits over a week. I want to print them out in a weekly calendar style of columns.
{
'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
数组.
It seems like using numpy
is the way to go to print this out in columns. I've also been able to "even out" the lists by adding some dummy strings, and turning the dictionary into an array of arrays. The thing I'm struggling with is how to convert the array of arrays into a proper numpy
array.
"Evened out"数组数组:
"Evened out" array of arrays:
[
['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:数组的索引过多
.我认为我需要做的是进入并将这些内部数组转换为numpy数组,然后将它们转换为 vstack
,但是我不清楚如何处理numpy.我也想知道我是否应该从头开始就不那么快地丢弃字典.
Which always errors IndexError: too many indices for array
. I think what I need to do is to go in and turn those inner arrays into numpy arrays then vstack
them, but I'm pretty unclear on how to handle numpy. I also wonder if I shouldn't be so quick to trash the dictionary from the start.
这是我要查找的内容的简化版本:
Here is a foreshortened version of what I'm looking for:
| Mon | Tue | Wed | Thu | Fri |
| 04:15 | 09:40 | 10:32| 04:12 | 11:00 |
| Move..|Do a ..|Add .. | Use ..| Change|
| 03:52 | | | | |
推荐答案
我认为您可以在不使用 numpy
且仅使用stdlib模块的情况下解决该问题!
I think you can solve this without numpy
and using only stdlib modules!
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 =''
或类似的设置默认值.
zip_longest
obviates the need to "even out" your arrays...it just returns None
where there was nothing to put. You could also pass fillvalue=''
or similar to set the default value.
您还可以使用有序字典来避免像我一样手动指定日期的顺序.
You could also use an ordered dict to avoid manually specifying the order of the days as I did.
现在您有了单独的行,剩下的只是漂亮打印中的一个练习. textwrap
模块可能是您的朋友.
Now that you have the individual rows, all that's left is an exercise in pretty-printing. The textwrap
module is probably your friend here.
这花了点功夫,但是这里也有漂亮的印刷品
This took a bit of doing, but here's the pretty printing taken care of as well
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
这篇关于从python字典打印列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!