-1

I am trying to create connect 4 game with 6/7 arrray in python, and i need column headers so that column 0 is named a, column 2 is named b, and so on. The purpose of this is for the moves to be initiated by typing 'a' (drops token in first column) 'b' (drops token in second) etc.... This is my code to create the array

def clear_board():
     board = np.zeros((6,7))
     return board
halfelf
  • 9,737
  • 13
  • 54
  • 63
john doe
  • 1
  • 1
  • 1
  • You can pass `dtype=int` to your `np.zeros` to make the output look much cleaner – user3483203 Dec 05 '18 at 05:45
  • Are you concerned about how the array is displayed, or just want a more intuitive way of referencing the columns? `numpy` doesn't actually label columns or rows. You could though define `a,b,c,.. = 0,1,2,...` and use `board[:, b]` to reference the 2nd column. – hpaulj Dec 05 '18 at 06:33

1 Answers1

1

If you need column names, the easiest way is to use a pandas Dataframe instead of a numpy array:

import pandas as pd 

def clear_board():
    board = pd.DataFrame(np.zeros((6,7)),columns=list('ABCDEFG'))
    return board

>>> clear_board()
     A    B    C    D    E    F    G
0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0  0.0  0.0  0.0
3  0.0  0.0  0.0  0.0  0.0  0.0  0.0
4  0.0  0.0  0.0  0.0  0.0  0.0  0.0
5  0.0  0.0  0.0  0.0  0.0  0.0  0.0

Beyond that, take a look at the options provided in this answer

sacuL
  • 49,704
  • 8
  • 81
  • 106