gpt4 book ai didi

python - 从 python 字典中打印列

转载 作者:行者123 更新时间:2023-12-01 03:29:26 26 4
gpt4 key购买 nike

我有一本一周内提交的字典。我想以每周日历样式的列将它们打印出来。

{
'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 数组。

“均衡”数组数组:

[ 
['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。我还想知道我是否不应该这么快从一开始就把字典扔掉。

这是我正在寻找的内容的缩短版本:

|  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 模块的情况下解决此问题!

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='' 或类似内容来设置默认值。

您还可以使用有序字典来避免像我一样手动指定日期的顺序。

现在您已经有了单独的行,剩下的就是 pretty-print 的练习了。 textwrap module可能是你的 friend 。

编辑:这需要一些工作,但这里也处理了 pretty-print

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 - 从 python 字典中打印列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41093776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com