來自基友的一個問題:用python實現輸入是一個二維list,一個可能的輸入如下所示:Input_list = [ [”china”,”is”,”a”,”great”,”country”], [“usa”,”always”,”korea”],[“korea”,”has”,”lots of”,”dat... ...
來自基友的一個問題:
用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列印到你的終端上,如附件中圖片所示:工整而且美觀! 列之間用豎線隔開 ,行之間用一連串的等號隔開;
我的代碼:
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))