如何将此列表打印成表格格式,yasix为垂直轴,x轴为水平轴,仅使用for循环?
data = [
104, 117, 130, 143, 156, 169, 182, 195, 208,
112, 126, 140, 154, 168, 182, 196, 210, 224,
120, 135, 150, 165, 180, 195, 210, 225, 240,
128, 144, 160, 176, 192, 208, 224, 240, 256,
136, 153, 170, 187, 204, 221, 238, 255, 272,
144, 162, 180, 198, 216, 234, 252, 270, 288,
152, 171, 190, 209, 228, 247, 266, 285, 304,
160, 180, 200, 220, 240, 260, 280, 300, 320]
xaxis = [1,2,3,4,5,6,7,8,9]
yaxis = [4,5,6,7,8,9,10,11]
与此类似,我不需要这些线。
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
| Sydney | 2058 | 4336374 | 1214.8 |
+-----------+------+------------+-----------------+
答案 0 :(得分:2)
如果你想完全靠自己做这件事,你可以使用here中的食谱并构建如下表格:
def line(l, sep="\t"):
return sep.join(str(item) for item in l)
print(line([""] + xaxis)) # table header
lenx = len(xaxis)
for y, xindex in enumerate(range(0, len(data), lenx)):
print(line([yaxis[y]] + data[xindex:xindex+lenx]))
结果:
1 2 3 4 5 6 7 8 9
4 104 117 130 143 156 169 182 195 208
5 112 126 140 154 168 182 196 210 224
6 120 135 150 165 180 195 210 225 240
7 128 144 160 176 192 208 224 240 256
8 136 153 170 187 204 221 238 255 272
9 144 162 180 198 216 234 252 270 288
10 152 171 190 209 228 247 266 285 304
11 160 180 200 220 240 260 280 300 320