你可以这样做:
dic = {'key1': [0, 0, 0], 'key2': [1, 1, 1], 'key3': [2, 2, 22222]}
def printTable (tbl, borderHorizontal = '-', borderVertical = '|', borderCross = '+'):
cols = [list(x) for x in zip(*tbl)]
lengths = [max(map(len, map(str, col))) for col in cols]
f = borderVertical + borderVertical.join(' {:>%d} ' % l for l in lengths) + borderVertical
s = borderCross + borderCross.join(borderHorizontal * (l+2) for l in lengths) + borderCross
print(s)
for row in tbl:
print(f.format(*row))
print(s)
sorted_keys=sorted(dic.keys(),reverse=True)
table=[sorted_keys]
for row in zip(*[dic[k] for k in sorted_keys]):
table.append(list(row))
printTable(table)
印刷
+-------+------+------+
| key3 | key2 | key1 |
+-------+------+------+
| 2 | 1 | 0 |
+-------+------+------+
| 2 | 1 | 0 |
+-------+------+------+
| 22222 | 1 | 0 |
+-------+------+------+