一個python二維列表格式化美觀輸出的題目


來自基友的一個問題:

用python實現

輸入是一個二維list,一個可能的輸入如下所示:

Input_list = [

        [”china”,”is”,”a”,”great”,”country”],

          [“usa”,”always”,”korea”],

[“korea”,”has”,”lots of”,”database”],

[“this’s”,”something”,”fantastic”]

]

就是list中的每個元素還是一個listx,這個listx中的每個元素是一個長度不定的字符串,

要求給定這樣一個輸入,然后盡可能工整 美觀的把這個list打印到你的終端上,如附件中圖片所示:工整而且美觀! 列之間用豎線隔開 ,行之間用一連串的等號隔開;

238f6460e19531b50ebdd520e828808

我的代碼:

Input_list = [["china","is","a","great","country"],
              ["usa","always","korea"],
              ["korea","has","lots of","database"],
              ["this's","something","fantastic"]]

max_size=max(max(len(j) for j in i) for i in Input_list)
max_col=max(len(i) for i in Input_list)

for i in range(len(Input_list)):
    for j in range(max_col):
        print(eval("'{: >"+str(max_size)+"}'.format(Input_list[i][j] if j < len(Input_list[i]) else '')"),end='|')
    print('\n'+'='*((max_size+1)*max_col))

效果

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM