把数组里的元素按照对其的表格列方式输出,可以指定列宽,每行三个
def columnar_display(list, pagewidth=77) : maxlen = 0 for item in list : l = len(str(item)) if l > maxlen : maxlen = l maxlen += 2 # space it out a little more numcol = int(pagewidth / maxlen) i = 0 for item in list : print '{0:{1}}'.format(item, maxlen), i += 1 if i % numcol == 0 : print '\n', list = [ 'Python Core', 'Python VTE', 'Regular Expression', 'socket', 'tarfile', 'Testing', 'threading', 'twitter', 'unittest', 'Upstart', 'Webkit', 'Zeitgeist' ] columnar_display(list)