October 22, 2024
Chicago 12, Melborne City, USA
python

Python 2d list initialization


#!/usr/bin/env python                                                           
                                                                                
                                                                                
BOARD_SIZE = 8  # square gaming board                                                                 
NOTHING = '.'                                                                   
SOMETHING = 'K'                                                                 
                                                                                
                                                                                
board = [[NOTHING] * BOARD_SIZE] * BOARD_SIZE  # indexing: (row, col)           
board[0][0] = SOMETHING                                                         
                                                                                
for row in board:                                                               
    for c in row:                                                               
        print(c, end='')                                                        
    print()

Now that I was expecting:

$ nano cownway.py &&./cownway.py 
K.......
K.......
K.......
K.......
K.......
K.......
K.......
K.......

Please explain in ‘C’ terms what is happening on line board = .



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video