2014-10-30 56 views
-1

假設我想輸出三行。每行對應不同的表格。這些列是鍵的相應值。在python中,如何從不同的字典中爲同一個鍵打印所有值?

這將使打印出垂直:

for i in list_of_keys: 
    print dict1[i], dict2[i], dict3[i] 

任何辦法,我創建一個簡單的方式爲彼此而不是頂部堆疊在字典中的值?要做到這一點

一種方法是這樣寫:

for i in list_of_keys: 
    print dict1[i], 
for i in list_of_keys: 
    print dict2[i], 

但這裏的問題是,如果詞典包含的值超過可容納在同一行,我得到多行的字典,直到字典用完然後在下面多行。通過定義列中值的排列鍵來排列字典的值。

+0

我我不確定你想要結果如何。你是否希望在行結束之前打印這些值?或者你想要每行有四個值? – justhalf 2014-10-30 06:15:59

+0

如果字典包含的值比可以放在一行更多的值,那麼我猜如果我能在新行開始之前設置一個預定義的值(如50個字符),那麼對於各種類型的字典來說,這將很好。 – dimb 2014-10-30 06:23:59

回答

0

假設Python 2,我寫了一個函數pprint_multiline,您可以自定義它。

可以自定義如下:

  • 線路長度
  • 項之間的分離器
  • 線之間的分離器(用於行的兩個塊,None之間空行爲沒有行分隔空字符串全部)
  • 各個項目的對齊,以及
  • 的對齊字符。

該函數接受水平打印的列表列表,因此您需要在調用此函數之前先解壓字典,如以下示例所示。

這適用於字典中的任何值,假設它實現了__str__函數(大多數Python對象都這樣做)。

功能:

def pprint_multilines(linelen=80, separator=' ', line_separator=None, 
         align='left', align_char=' ', lists=[]): 
    maxlen = max(len(lst) for lst in lists) 
    printed_lines = ['']*len(lists) 
    empty = True 
    printed = False 
    for i in xrange(maxlen): 
     max_item_len = max(len(str(lst[i])) for lst in lists) 
     if max_item_len + len(printed_lines[0]) + 1 > linelen: 
      if printed and line_separator is not None: 
       if len(line_separator) > 0: 
        print line_separator*(linelen/len(line_separator)) 
       else: 
        print 
      for printed_line in printed_lines: 
       print printed_line 
      printed = True 
      printed_lines = ['']*len(lists) 
      empty = True 
     for lst_idx, lst in enumerate(lists): 
      if not empty: 
       printed_lines[lst_idx] += separator 
      if align == 'left': 
       printed_lines[lst_idx] += str(lst[i]).ljust(max_item_len, align_char) 
      else: # align == 'right' 
       printed_lines[lst_idx] += str(lst[i]).rjust(max_item_len, align_char) 
     else: 
      empty = False 
    if not empty: 
     if printed and line_separator is not None: 
      if len(line_separator) > 0: 
       print line_separator*(linelen/len(line_separator)) 
      else: 
       print 
     for printed_line in printed_lines: 
      print printed_line 

要看到它在行動,你可以看到下面的例子:

dict1 = {'a': 'first', 'b': 'second', 'c': 3, 'd': ['the', 'fourth']} 
dict2 = {'a': 1, 'b':2, 'c':3, 'd':4} 
dict3 = {'a': '1', 'b':'two', 'c':'three', 'd': 'fourth'} 
list_of_keys = ['a','b','c','d'] 

pprint_multilines(linelen=15, separator=' ', align='left', align_char=' ', 
        line_separator='-', 
        lists=[[dict1[key] for key in list_of_keys], 
         [dict2[key] for key in list_of_keys], 
         [dict3[key] for key in list_of_keys]]) 
print 

pprint_multilines(linelen=15, separator=' ', align='left', align_char=' ', 
        lists=[[dict1[key] for key in list_of_keys], 
         [dict2[key] for key in list_of_keys], 
         [dict3[key] for key in list_of_keys]]) 
print 

pprint_multilines(linelen=25, separator=' ', align='left', align_char=' ', 
        line_separator='', 
        lists=[[dict1[key] for key in list_of_keys], 
         [dict2[key] for key in list_of_keys], 
         [dict3[key] for key in list_of_keys]]) 
print 

pprint_multilines(linelen=45, separator=' ', align='left', align_char='_', 
        lists=[[dict1[key] for key in list_of_keys], 
         [dict2[key] for key in list_of_keys], 
         [dict3[key] for key in list_of_keys]]) 
print 

pprint_multilines(linelen=45, separator=' ', align='right', align_char=' ', 
        lists=[[dict1[key] for key in list_of_keys], 
         [dict2[key] for key in list_of_keys], 
         [dict3[key] for key in list_of_keys]]) 

將輸出(有一些評論):

 
first second 
1  2 
1  two 
--------------- <-- This is the line separator 
3     <-- This next item fits in 15 chars, but 
3      if appended to the previous line 
three     this item won't fit, so break the third item to new lines 
--------------- <-- Note that this spans the specified line length 
['the', 'fourth'] 
4 
fourth 

first second 
1  2 
1  two 
3     <-- No line separator! 
3 
three 
['the', 'fourth'] 
4 
fourth 

first second 3  <-- linelen=25, the third item now fits! 
1  2  3 
1  two three 
        <-- A blank line separator 
['the', 'fourth'] 
4 
fourth 

first second 3____ ['the', 'fourth'] <-- Showing alignment chars 
1____ 2_____ 3____ 4________________ <-- linelen=45, everything fits! 
1____ two___ three fourth___________ 

first second  3 ['the', 'fourth'] <-- These blocks are right-aligned 
    1  2  3     4 
    1 two three   fourth 
相關問題