1

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?

1

7 Answers 7

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

so what happens if i use one??? i saw in your hints to use one that is why i used. May be you should concentrate more on the answer rather than its beauty
got it, i didnt read your instructions fully just skimmed sorry:) , kept to the rules this time while answering other question
0
for i in range(10):
    for j in range(10):
        print(locate[i][j])`

This should make it work.

4 Comments

what if the list is not 10X10? i think you should use len(locate) there
@DonatPants this is just to highlight the problem in the code, of course you should use the len(localte).
I've tried this [code] 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][j].append(0) for i in range(10): for j in range(10): print(locate[i][j]) [code] It still won't work
I don't think using an index is useful if you just want to loop over a list. Just a for elt in list: then do_something(elt) seems better than for i in len(list): followed by do_somethin(list[i])
0

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

1 Comment

as locate is a 2D array, then an element of locate will be a 1D array, so locate[i] is a 1D array, and as such adding an element to it is perfectly fine. Moreover, a list can contain different types of data, in Python.
0

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

Comments

0

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)

Comments

0

You have many forms of printing

Using numpy

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

Using pprint

import pprint
pprint.pprint(matrix)

1 Comment

neither of those prints the matrix correctly if it is big enough (more than 5 columns)
0

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

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

3 Comments

The only problem is that the characters have got to be strings.
What is row in the context of the question?
I suggest for row in board: print(*row) (squeezed in one line because of comment formatting limitations) to make it work for non-string elements. *row unpacks row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.