我觉得这段代码太过分了 - 它怎么能缩短呢?我是一个初学者,所以忍受着我。如何更有效地编写此代码?
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/ –