0

I am trying to format two 2D numpy arrays to have a similar format to this: Table Format

Currently, I have transposed the 2 arrays to look like coordinates:

##Transposes both arrays into coordinates
gameSet = np.array((player1,player2)).T

Then I have iterated through the array to get the similar form using:

##iterates through 2D numpy array
for player1 in range(0, row-1):
    print(" ")
    for player2 in range(0, col-1):
        print (gameSet[player1,player2], end = "")

With the code above, I get the similar format but I would like for it to have the A1...An and B1....Bn on the axis'. Any idea how I could do that or a package that may help with the formatting? Anything helps, thank you!

  • 2
    You mean, do a `print(' B1 B2 B3 B4 ...')` with proper spacing? – hpaulj Mar 17 '19 at 04:16
  • This is a hack, but what if you just added "A1", "A2", etc. to the player1 array, and "B1", "B2", etc. to the player2 array? This would not give you the nice table borders, but it would give you the correct spacing. And you can use a [table-printing package](https://stackoverflow.com/a/9713042/7315159) to format your data nicely. – Niayesh Isky Mar 17 '19 at 05:54
  • @hpaulj yes, I could print B1, B2,... but the rows and columns change depending on user input so how could I set it to fill depending on the user input for both A and B and with proper spacing? – Bryan Camacho Mar 17 '19 at 19:52
  • Python formatting, either the older % style or new `.format` has the tools for displaying strings and numbers with a specific spacing. `np.savetxt`, it's `csv` writer, uses the `%` style. Yes, using this can be a bit tedious. `pandas` may have some fancier layout tools, but I haven't used those. – hpaulj Mar 17 '19 at 20:24

1 Answers1

0

An illustration of how to use basic Python formatting

An array:

In [188]: arr = np.arange(12).reshape(3,4)                                                

Generate labels:

In [189]: rlbl = ['A{}'.format(i) for i in range(arr.shape[0])]                           
In [190]: clbl = ['B{}'.format(i) for i in range(arr.shape[1])]                           

Formatting strings:

Top row:

In [192]: tfmt = ' ~   ||'+'|'.join(['%10s']*arr.shape[1])                                
In [193]: tfmt%tuple(clbl)                                                                
Out[193]: ' ~   ||        B0|        B1|        B2|        B3'

data row:

In [194]: rfmt = '%5s||'+'|'.join(['%10d']*arr.shape[1])                                  
In [195]: rfmt%(rlbl[0], *tuple(arr[i,:]))                                                
Out[195]: '   A0||         4|         5|         6|         7'

Collect rows:

In [201]: astr = [_193]                                                                   
In [202]: for i in range(arr.shape[0]): 
     ...:     astr.append(rfmt%(rlbl[i], *tuple(arr[i,:]))) 
     ...:

and print:

In [203]: print('\n'.join(astr))                                                          
 ~   ||        B0|        B1|        B2|        B3
   A0||         0|         1|         2|         3
   A1||         4|         5|         6|         7
   A2||         8|         9|        10|        11
hpaulj
  • 221,503
  • 14
  • 230
  • 353