【问题标题】:print columns from python dictionary从 python 字典中打印列
【发布时间】:2017-04-26 21:37:45
【问题描述】:

我有一个超过一周的提交字典。我想以每周日历样式的列打印出来。

{
  '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: 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 |       |       |       |       |

【问题讨论】:

  • 您的方法在 numpy 版本 1.11.1 上非常适合我。你用的是旧的 numpy 吗?

标签: python arrays numpy dictionary


【解决方案1】:

我认为您可以在不使用 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          

【讨论】:

  • 虽然这肯定可以更好地填充数组,并且 textwrap 会派上用场,但我仍然认为我需要 numpy 来跨数组打印。
  • 哇,我绝对不会到达那里。谢谢。
  • @icicleking 如果您获得大量输入,可能会有所帮助的一件事是将wrapped_commitsjustified_commits 中的列表推导式更改为带有(wrapper.wrap(commit) for commit in v) 或的生成器表达式相似的。我刚试过这个,它产生了相同的输出。
猜你喜欢
  • 2017-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 2015-07-05
  • 1970-01-01
  • 2016-09-03
相关资源
最近更新 更多