2016-09-30 71 views
2

我已經寫以下代碼:漂亮印刷(2D陣列,盒)

for row in range(len(listOfLists)): 
    print('+' + '-+'*len(listOfLists)) 
    print('|', end='') 
    for col in range(len(listOfLists[row])): 
     print(listOfLists[row][col], end='|') 
    print(' ') #To change lines 
print('+' + '-+'*len(listOfLists)) 

輸入:

[['a', 'b', 'c'], 
['d', 'e', 'f'], 
['g', 'h', 'i'], 
['j', 'k', 'l']] 

輸出:

+-+-+-+-+ 
|a|b|c| 
+-+-+-+-+ 
|d|e|f| 
+-+-+-+-+ 
|g|h|i| 
+-+-+-+-+ 
|j|k|l| 
+-+-+-+-+ 

所需的輸出:

+-+-+-+ 
|a|b|c| 
+-+-+-+ 
|d|e|f| 
+-+-+-+ 
|g|h|i| 
+-+-+-+ 
|j|k|l| 
+-+-+-+ 

哪個打印 '+ - +' 的2D陣列的元件周圍。 但是,我的代碼只適用於方陣(n^2)。

我怎麼能概括它,這樣它適用於陣列中的任何變化(只要所有列表的長度相等)

謝謝

+0

請縮進代碼 –

+1

你能不能請,正確的格式代碼,並把例如你會得到什麼,你想要什麼? –

+0

@vishes_shell那也是:-) –

回答

2

len(listOfLists)用於的大小您的問題在兩個方向上打印桌子。 len(listOfLists)默認爲行數,做len(listOfLists[0])你得到的列數。

listOfLists = [['a', 'b', 'c'], 
    ['d', 'e', 'f'], 
    ['g', 'h', 'i'], 
    ['j', 'k', 'l']] 

for row in range(len(listOfLists)): 
    print('+' + '-+'*len(listOfLists[0])) 
    print('|', end='') 
    for col in range(len(listOfLists[row])): 
     print(listOfLists[row][col], end='|') 
    print(' ') #To change lines 
print('+' + '-+'*(len(listOfLists[0]))) 

輸出:

+-+-+-+ 
|a|b|c| 
+-+-+-+ 
|d|e|f| 
+-+-+-+ 
|g|h|i| 
+-+-+-+ 
|j|k|l| 
+-+-+-+ 

編碼愉快!

+0

如果listOfLists = [[1,2,3]],您的代碼將中斷 –

+0

良好的捕獲。固定爲該帳戶 –

+0

@ 345243lkj非常感謝你!:) – Saadat

1
def awesome_print(listOfLists): 
    for row in range(len(listOfLists)): 
     print('+' + '-+'*len(listOfLists[row])) 
     print('|', end='') 
     for col in range(len(listOfLists[row])): 
      print(listOfLists[row][col], end='|') 
     print(' ') #To change lines 
    print('+' + '-+'*len(listOfLists[row])) 

awesome_print([[1,2,3], [1,2,3], [2,3,0], [2,3,4]]) 

輸出

+-+-+-+ 
|1|2|3| 
+-+-+-+ 
|1|2|3| 
+-+-+-+ 
|2|3|0| 
+-+-+-+ 
|2|3|4| 
+-+-+-+ 

如果您需要與子陣列

的非固定大小的打印數據
def awesome_print2(listOfLists): 
    for row in range(len(listOfLists)): 
     print('+' + '-+'*len(listOfLists[row])) 
     print('|', end='') 
     for col in range(len(listOfLists[row])): 
      print(listOfLists[row][col], end='|') 
     print() 
     print('+' + '-+'*len(listOfLists[row])) 
awesome_print2([[1,2,3,5], [1,2,3], [2,3,0,6,3], [2,3,4]]) 

輸出:

+-+-+-+-+ 
|1|2|3|5| 
+-+-+-+-+ 
+-+-+-+ 
|1|2|3| 
+-+-+-+ 
+-+-+-+-+-+ 
|2|3|0|6|3| 
+-+-+-+-+-+ 
+-+-+-+ 
|2|3|4| 
+-+-+-+ 
+0

這似乎有每行之間的額外空間。最後的打印語句應該在for循環之外以避免這種情況(除非這是故意的) –

+0

非常感謝您的幫助! :) – Saadat

+0

@ 345243lkj固定,謝謝! –

2

要打印此分隔基於在行數上,不是合數lumns。使用其他測試用例可以幫助您立即進行調試

def printListOfLists(listOfLists): 
    for row in range(len(listOfLists)): 
     print('+' + '-+' * len(listOfLists[0])) 
     print('|', end='') 
     for col in range(len(listOfLists[row])): 
      print(listOfLists[row][col], end='|') 
     print(' ') # To change lines 
    print('+' + '-+' * len(listOfLists[0])) 


printListOfLists([ 
    ['a', 'b', 'c'], 
    ['d', 'e', 'f'], 
    ['g', 'h', 'i'], 
    ['j', 'k', 'l'] 
]) 

printListOfLists([['a', 'b', 'c']]) 

printListOfLists([['a'], ['b'], ['c']]) 

所有產生的結果目前預計:

+-+-+-+ 
|a|b|c| 
+-+-+-+ 
|d|e|f| 
+-+-+-+ 
|g|h|i| 
+-+-+-+ 
|j|k|l| 
+-+-+-+ 


+-+-+-+ 
|a|b|c| 
+-+-+-+ 


+-+ 
|a| 
+-+ 
|b| 
+-+ 
|c| 
+-+ 
+0

非常感謝您的幫助! – Saadat