我覺得這段代碼太過分了 - 它怎麼能縮短呢?我是一個初學者,所以忍受着我。如何更有效地編寫此代碼?
The problem statement is this (from Automate the Boring stuff)
而且我的代碼:
#printtable() function - will take string list and display in rjustified table
tabledata = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def printtable():
colwidths = [0] * len(tabledata)
strlen = 0
#find parameter for rjust
for i in range(len(tabledata)):
for k in range(len(tabledata[i])):
wordlength = (len(tabledata[i][k]))
if wordlength > strlen:
colwidths[i] = wordlength
strlen = wordlength
maxword = max(colwidths)
#print as table : 'invert'
x=0
while x<int(len(tabledata[0])):
for i in range(len(tabledata)):
print(tabledata[i][x].rjust(maxword, ' '), end=''),
x+=1
print('\n')
printtable()
在一般情況下,我怎麼能開始學會更有效地編碼?我想我可以提前開始流程圖 - 因爲通常我只是開始寫作並更換現場的東西。我覺得我的所有代碼都很難看,所以我希望有任何提示。謝謝!
這應該在:http://codereview.stackexchange.com/ –