Kujo
Kujo

Reputation: 17

Printing 2D-array in a grid

I need to print this to look like:

....

....

....

currently I have this:

def main():
    rows=3
    col=4
    values=[[0,0,0,0],
            [0,0,0,0],
            [0,0,0,0]]
    for i in range(rows):
        for j in range(col):
            values[i][j]='.'
    print(values)
main()

which will print [['.', '.', '.', '.'], ['.', '.', '.', '.'], ['.', '.', '.', '.']]

Is there a way to get it looking nicer?

Upvotes: 1

Views: 17677

Answers (6)

Alex Nikiporenko
Alex Nikiporenko

Reputation: 61

I would use *, the unpacking operator:

array_2d = [[1, 2, 3], [10, 20, 30], [100, 200, 300]]

for row in array_2d:
    print(*row, sep="\t")

#output:
1   2   3
10  20  30
100 200 300

Upvotes: 6

bigjake
bigjake

Reputation: 149

If you don't need, or don't want to use an array or a for loop for that matter, here are a bunch of ways to do it.

# Dynamic and easy to customize, and also what I would use if I needed
# to print more than once.
def print_chars(char=".",n_of_chars=4,n_of_lines=4):
    single_line = (char * n_of_chars) + '\n'
    print(single_line * n_of_lines)

print_chars()

....
....
....
....

# or maybe you want 2 rows of 10 dashes?
print_chars('-',10,2)

----------
----------

# 2 rows of 5 smileys?
print_chars(':-) ',5,2)

:-) :-) :-) :-) :-) 
:-) :-) :-) :-) :-)


# If your only going to use it once maybe this
print((('.' * 4) + '\n') * 4)

# or this
print('....\n' * 4)

There is probably a way to do it faster or more pythonic, but hey. In the end your needs or coding style may be different, and i'll bet there are probably many more ways to do this exact same thing with python. You just have to remember that readability and speed are both your friends, but oftentimes they don't like each other.(although simple things like this are almost always easy to read in python).

well there's my 2 cents. :-)

Upvotes: 0

Hackaholic
Hackaholic

Reputation: 19733

you can try like this:

for x in range(3):
    print('.'*4)         # when you multiply a string with n, it produces n string

output

....
....
....

modification in your code:

def main():
    rows=3
    col=4
    values=[[0,0,0,0],
        [0,0,0,0],
        [0,0,0,0]]
    for i in range(rows):
        print("".join('.' for j in range(col)))

main()

output:

....
....
....

Upvotes: 2

Michael Laszlo
Michael Laszlo

Reputation: 12239

This will work for any size of array with any type of values:

print('\n'.join(' '.join(str(x) for x in row) for row in values))

Somewhat longer and much clearer:

  lines = []
  for row in values:
    lines.append(' '.join(str(x) for x in row))
  print('\n'.join(lines))

Upvotes: 4

abarnert
abarnert

Reputation: 365657

If you have no need for the values object, and were only building it in an attempt to make it easier to print… don't do that, just use Hackaholic's solution.

But if you actually need the values object, or you already have it and want to know how to print it, do it like this:

print('\n'.join(''.join(row) for row in values))

Or, more explicitly:

for row in values:
    line = ''.join(row)
    print(line)

Or, even more explicitly:

for row in values:
    for col in row:
        print(col, end='')
    print()

Upvotes: 0

Cheluis
Cheluis

Reputation: 1412

You are printing a variable, which is an array, you need to print while on the loop. and you don't need an array full of 0.

for i in range (0, 5):
    for j in range (0, 5):
        print ".",
    print "\n"

Upvotes: -2

Related Questions