Wadzanai Mufunde
Wadzanai Mufunde

Reputation: 843

How do I print a 2d array in python

I'm having problems printing this 2d array for my Battleship game.

class Battleship:

    def __init__(self):
       self.__init__()

    def board(self, locate):
        locate = []
        for i in range(10):
            locate.append([])
            for j in range(10):
                locate[i].append(0)

        for i in range(10):
            for j in range(10):
                locate[i][j] = '%s,%s'%(i,j)
                print(locate)

I found how to initialise the array here: How to initialise a 2D array in Python? This is where I found the code sample to iterate over the 2d array, but it doesn't work for me: Iterating over a 2 dimensional python list

Can you give me some help please?

Upvotes: 1

Views: 22343

Answers (7)

Ryan Rana
Ryan Rana

Reputation: 1

This is the easiest way I use to print a list nicely.

for row in board:
    print(' '.join(row))

Upvotes: 0

Sanjit Mathew
Sanjit Mathew

Reputation: 11

Your string formatting is wrong %s is for string your i and j are integers so use %d

So use:

locate[i][j]='%d%d'%(i,j)

Further, to print in matrix format the full code would be:

for i in range(10):
        s=''
        for j in range(10):
            locate[i][j] = '%d,%d'%(i,j)
            s+=locate[i][j]+' '
        print s

Upvotes: 1

George C
George C

Reputation: 1223

You have many forms of printing

Using numpy

import numpy as np
print(np.matrix(matrix))

Using pprint

import pprint
pprint.pprint(matrix)

Upvotes: 0

HolyDanna
HolyDanna

Reputation: 629

First, let's clean up the creation of the 2D-list :

locate = [[str(i)+","+str(j) for j in range(10)] for i in range(10)]

Then, to iterate over the array and print the values stored in each case :

for locate_i in locate:
    for locate_i_j in locate_i:
        print locate_i_j

Explanation : when you use a for X in list, X will have the value of each element in the list. So, our locate_i is each of the sub-list of a given i index. When we loop over this list, we get the content, "%s,%s"%(i,j)

Upvotes: 0

Donat Pants
Donat Pants

Reputation: 369

here is some code i wrote to generate the multiplication board, in order to demonstrate how to print a 2d array of unknowen size

l = [] # empty list
a,b = 10,10 # size of board aXb
for i in range(1,a+1): # generate board
    temp = []
    for j in range(1,b+1):
        temp.append(i*j)
    l.append(temp)

for i in l: # print the board
    print i

the output is

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
[4, 8, 12, 16, 20, 24, 28, 32, 36, 40]
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
[6, 12, 18, 24, 30, 36, 42, 48, 54, 60]
[7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
[8, 16, 24, 32, 40, 48, 56, 64, 72, 80]
[9, 18, 27, 36, 45, 54, 63, 72, 81, 90]
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

here is a different way to print

for i in l: # print the board
    for j in i:
        print j,
    print

the output

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

Upvotes: 0

Craig Douglass
Craig Douglass

Reputation: 74

how do you want to print it? Please be more informative regarding the output format.

Assuming you want that format, i.e. xOy coordinates (x,y):

In this for-loop:

for i in range(10):
        locate.append([])
        for j in range(10):
            locate[i].append(0)

your code might fail because of this line:

locate[i].append(0)

The problem here is that your locate variable is a 2D array, so you might want to try something like:

locate[i][j].append(0)

Otherwise, you will have to append a full List, if I'm not mistaken

Also, your last statement "print(locate)" should be outside the for-loop to print the matrix only once

Upvotes: 0

limbo
limbo

Reputation: 702

for i in range(10):
    for j in range(10):
        print(locate[i][j])`

This should make it work.

Upvotes: 0

Related Questions