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?
asked Nov 26, 2014 at 2:01
4
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]]
answered Nov 26, 2014 at 2:18
Michael LaszloMichael Laszlo
11.7k2 gold badges27 silver badges46 bronze badges
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:
....
....
....
answered Nov 26, 2014 at 2:07
HackaholicHackaholic
18.1k3 gold badges52 silver badges70 bronze badges
0
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
answered Oct 9, 2021 at 16:05
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[]
answered Nov 26, 2014 at 2:15
abarnertabarnert
341k44 gold badges573 silver badges651 bronze badges
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. :-]
answered Nov 26, 2014 at 18:55
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"
answered Nov 26, 2014 at 2:09
CheluisCheluis
1,3723 gold badges20 silver badges50 bronze badges
4