一個(gè)python二維列表格式化美觀輸出的題目
來自基友的一個(gè)問題:
用python實(shí)現(xiàn)
輸入是一個(gè)二維list,一個(gè)可能的輸入如下所示:
Input_list = [
[”china”,”is”,”a”,”great”,”country”],
[“usa”,”always”,”korea”],
[“korea”,”has”,”lots of”,”database”],
[“this’s”,”something”,”fantastic”]
]
就是list中的每個(gè)元素還是一個(gè)listx,這個(gè)listx中的每個(gè)元素是一個(gè)長度不定的字符串,
要求給定這樣一個(gè)輸入,然后盡可能工整 美觀的把這個(gè)list打印到你的終端上,如附件中圖片所示:工整而且美觀! 列之間用豎線隔開 ,行之間用一連串的等號(hào)隔開;

我的代碼:
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))
效果

作者:九命貓幺
博客出處:http://www.rzrgm.cn/yongestcat/
歡迎轉(zhuǎn)載,轉(zhuǎn)載請標(biāo)明出處。
如果你覺得本文還不錯(cuò),對你的學(xué)習(xí)帶來了些許幫助,請幫忙點(diǎn)擊右下角的推薦
博客出處:http://www.rzrgm.cn/yongestcat/
歡迎轉(zhuǎn)載,轉(zhuǎn)載請標(biāo)明出處。
如果你覺得本文還不錯(cuò),對你的學(xué)習(xí)帶來了些許幫助,請幫忙點(diǎn)擊右下角的推薦

浙公網(wǎng)安備 33010602011771號(hào)