Skip to content

Multidimensional array in python

#!/usr/bin/python 
rows=5;cols=2 
players=[[0]*cols for _ in xrange(rows)] 
print "####### Print Original Array ###################" 
print players 
print "\n" 

print "####### Direct Access ###################" 
print "going to set [0][0]=S9 and [3][1]=D3" 
players[0][0]='S9' 
players[3][1]='D3' 
print players 
print "\n" 

print "####### Append Col ###################" 
print "going to add to [2] value C7, and add to [4] value S4" 
players[2].append('C7') 
players[4].append('S4') 
print players 
print "\n" 

print "####### Append Row ###################" 
print "going to add row [5]" 
players.extend([[0]*cols]) 
print players 
print "\n" 

print "####### Print Complete Rows ###################" 
for row in range(len(players)): 
    print players[row] 
print "\n" 

print "####### Print item for item, by Column by Row ######" 
for row in range(len(players)): 
    for col in range(len(players[row])): 
        print str(players[row][col]).rjust(10), 
    print

REF: http://stackoverflow.com/questions/261006/multidimensional-array-python

Published inPython