-1

How can i print out this list into a tabular format with yasix as the vertical axis and x axis as the horizontal axis only using for loops ?

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]

Similar to this, i don't need the lines.

+-----------+------+------------+-----------------+
| 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     |
+-----------+------+------------+-----------------+
Tamdim
  • 972
  • 2
  • 12
  • 29
  • [`str.format`](https://docs.python.org/3.1/library/string.html#format-string-syntax) is your friend. – Adam Smith May 29 '14 at 04:50
  • 1
    Your example is from [PreatyTables](https://code.google.com/p/prettytable/). So why not use this? – Marcin May 29 '14 at 04:51
  • 1
    Um, you pulled your sample directly from prettytable, so i would recommend...using prettytable https://code.google.com/p/prettytable/wiki/Tutorial – Tadgh May 29 '14 at 04:52
  • The OP is asking how to do this with `for` loops, so he should probably look at the source code of `prettytable`. – merlin2011 May 29 '14 at 05:13
  • @merlin2011: Didn't see the for loops, oops. – Dair May 29 '14 at 05:18

1 Answers1

2

If you want to do this entirely on your own, you could use the recipe from here and construct the tables like this:

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]))

Result:

        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
Community
  • 1
  • 1
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561