Notes learning python "python programming quick start - let automate the tedious work" of the five

Problem five:
Write a function called printTable (), which accepts a list of lists of strings, it will be displayed in well-organized tables, each column of right-aligned. It assumed that all the inner list contains the same number of strings. For example, the value may look like this:
TableData = [[ 'Apples', 'oranges', 'Cherries', 'Banana'],
[ 'Alice', 'Bob', 'Carol', 'David'],
[ 'dogs', 'cats', 'moose', 'goose']]
your printTable () function will print out:
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']
	]
	#思路:首先必须找到每个内层列表中最长的字符串,这样整列就有足够的宽度以放下所有字符串。你可以将每一列的最大宽度,保存为一个整数的列表。
#存储字符串长度
col=[len(k) for i in tableData for k in i ]
for i in zip(*tableData):
	for k in i:
		print(k.rjust(max(col)),end='')
	print()#打印一行以后换行

Published 23 original articles · won praise 5 · Views 389

Guess you like

Origin blog.csdn.net/weixin_43287121/article/details/104482209