简体   繁体   English

在网格中打印二维数组

[英]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?有没有办法让它看起来更好?

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: 输出:

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

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

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))

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 . 如果您不需要values对象,而只是为了使其更易于打印而构建它...不用这样做,只需使用Hackaholic的解决方案即可

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: 但是,如果您实际上需要values对象,或者您已经拥有它并且想知道如何打印它,请按照以下方式进行操作:

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()

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. 如果您不需要,或者不想使用数组或for循环,这里有很多方法。

# 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. 可能有一种方法可以使它更快或更Pythonic,但是嘿。 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. 最后,您的需求或编码风格可能会有所不同,我敢打赌,可能还有许多其他方法可以使用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). 您只需要记住可读性和速度都是您的好朋友,但通常他们彼此不喜欢(尽管像这样的简单事情几乎总是很容易在python中阅读)。

well there's my 2 cents. 好吧,有我的2美分。 :-) :-)

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. 并且您不需要全为0的数组。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM