在這一節,他們希望我們創建該表:自動化無聊的東西第6章表打印機幾乎完成
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
它必須是合理的權利,並輸入資料表。這裏是我的代碼:
tableData=[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
listlens=[]
tour=0
lists={}
for m in tableData:
total=0
tour+=1
for n in m:
total+=len(n)
lists["list:",tour]=total
print("list",tour,total)
itemcount=list(lists.values())
sortedlen=(sorted(itemcount,reverse=True))
longest=sortedlen[0]
#print (lists['list:', 1])
#print (longest)
for m in range(len(tableData[0])):
for n in range(len(tableData)):
print (tableData[n][m],end=" ")
n+=1
print ("".rjust(lists['list:', 1],"-"))
m+=1
我差不多完成了,除了一件事,我無法使它對正。這個輸出是我到目前爲止最接近的。
apples Alice dogs ---------------------------
oranges Bob cats ---------------------------
cherries Carol moose ---------------------------
banana David goose ---------------------------
如果我把rjust內for循環裏面的輸出有很大不同:
apples-------------------------- Alice-------------------------- dogs--------------------------
oranges-------------------------- Bob-------------------------- cats--------------------------
cherries-------------------------- Carol-------------------------- moose--------------------------
banana-------------------------- David-------------------------- goose--------------------------
您是否嘗試過加盟第一? –
你能多解釋一下嗎?你的意思是在打印函數的結尾還是在for循環中? –
我現在試過了,但得到了參數錯誤。 –