簡體   English   中英

從python字典打印列

[英]print columns from python dictionary

我有一周以上的提交字典。 我想以每周日歷樣式的列打印出來。

{
  '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而僅使用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模塊可能是您的朋友。

編輯:這花了點功夫,但是這也是漂亮的印刷品

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          

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM