标签:car ble ring rri print orange 存储 dog oranges
1 tableData = [[‘apple‘,‘oranges‘,‘cherries‘,‘banana‘], 2 [‘Alice‘,‘Bob‘,‘Carol‘,‘David‘], 3 [‘dogs‘,‘cats‘,‘moose‘,‘goose‘]] 4 def printTable(Datas): 5 #创造了一个列表用于存储每个小列表的最大长度 6 colWidths = [0]*len(Datas) 7 #读取每个小列表中的字符,筛选出最大长度,并存储在colWidths列表中 8 for i in range(len(Datas)): 9 for j in range(len(Datas[i])-1) : 10 if len(Datas[i][j+1]) >= len(Datas[i][j]) : 11 lennum = len(Datas[i][j+1]) 12 colWidths[i] = lennum 13 #读取每一个字符串,用右对齐的方法,排列打印 14 for i in range(len(Datas[i])): 15 for j in range(len(Datas)): 16 string = Datas[j][i] 17 space = colWidths[j]+2 18 if j == (len(Datas)-1): 19 print(string.rjust(space)) 20 else: 21 print(string.rjust(space),end = ‘‘) 22 printTable(tableData)
标签:car ble ring rri print orange 存储 dog oranges
原文地址:https://www.cnblogs.com/huzhikai001/p/13149713.html