如何打印python列表的元素看起来像一个矩阵[复制]

时间:2021-10-14 21:41:15

This question already has an answer here:

这个问题在这里已有答案:

I have users inputting into my program the degree of a matrix (e.g 3x3 == X=3, Y=3) I then populate a list of 9 values that will make up my 3x3 matrix like so:

我有用户在我的程序中输入矩阵的程度(例如3x3 == X = 3,Y = 3)然后我填充了9个值的列表,这些值将组成我的3x3矩阵,如下所示:

    total = x * y  # the x and y values inputted by the user
    i = 1
    while i <= total:
        matrix.append(i)
        i += 1

After I have this matrix populated I want to print it out such that I print values matrix[0], [1], [2] in the first row and so on. But I want to do this for any size matrix, I have tried this:

在我填充了这个矩阵之后,我想将它打印出来,以便在第一行打印值matrix [0],[1],[2],依此类推。但我想为任何大小的矩阵做这个,我试过这个:

i = 1
j = 0
while j < total:
    print matrix[j]
    if not i == x:
        print ''
        i += 1
    else:
        print '\n'
        i = 1
    j += 1

With no luck as it prints each value on a new line regardless... Any help is appreciated, thanks!

没有运气,因为它打印新线上的每个值,无论如何...感谢任何帮助,谢谢!

3 个解决方案

#1


1  

How about this:

这个怎么样:

x = 3
y = 3

matrix = [[(i+1)+(j*x) for i in range(x)] for j in range(y)]

for row in matrix:
    print ' '.join(map(str, row))

prints:

打印:

1 2 3
4 5 6
7 8 9

EDIT: as suggested in the comments, we can make this last line more flexible to show a proper "matrix" format as follows:

编辑:正如评论中所建议的那样,我们可以使这最后一行更灵活,以显示适当的“矩阵”格式,如下所示:

for row in matrix:
    print ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row))

This means that regardless of the size of x and y, we'll always space our values out evenly. For example if x=6 and y=6, it'll print:

这意味着无论x和y的大小如何,我们都会始终均匀地分配我们的值。例如,如果x = 6且y = 6,它将打印:

 1  2  3  4  5  6
 7  8  9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36

if x=10 and y=10 it'll print:

如果x = 10且y = 10,它将打印:

  1   2   3   4   5   6   7   8   9  10
 11  12  13  14  15  16  17  18  19  20
 21  22  23  24  25  26  27  28  29  30
 31  32  33  34  35  36  37  38  39  40
 41  42  43  44  45  46  47  48  49  50
 51  52  53  54  55  56  57  58  59  60
 61  62  63  64  65  66  67  68  69  70
 71  72  73  74  75  76  77  78  79  80
 81  82  83  84  85  86  87  88  89  90
 91  92  93  94  95  96  97  98  99 100

and so on.

等等。

#2


0  

You can replace your

你可以替换你的

print ""

with

print "",

in order to not add a newline to the end.

为了不在最后添加换行符。

Alternatively, you could use the python 3 print function, which supports this as an argument. To do this, you should import __ future__ and call print like a function. See the code below as an example:

或者,您可以使用python 3 print函数,它支持将此作为参数。为此,您应该导入__ future__并像函数一样调用print。请参阅以下代码作为示例:

from __future__ import print_function
print("my string", end = "")

You can read more about using print as a function on the docs page.

您可以在文档页面上阅读有关将print用作函数的更多信息。

#3


0  

This answer is based on this answer. That answer takes into account only the shape. To make it look like one matrix you can use box-drawing characters.

这个答案是基于这个答案。该答案仅考虑形状。为了使它看起来像一个矩阵,你可以使用盒子绘图字符。

c, x, y = 1, 3, 3

matrix = [[(i+1)+(j*x) for i in range(x)] for j in range(y)]

for row in matrix:
    if c == 1:
        print u'\u250F', ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row)), u'\u2513'
    elif c-y == 0:
        print u'\u2517', ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row)), u'\u251B'
    else:
        print u'\u2503', ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row)), u'\u2503'
    c += 1

Another possibility is:

另一种可能性是:

x, y = 3, 3

matrix = [[(i+1)+(j*x) for i in range(x)] for j in range(y)]

print u'\u250F', ''.join(((2*x)-2)*' '), u'\u2513'
for row in matrix:
    print u'\u2503', ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row)), u'\u2503'
print u'\u2517', ''.join(((2*x)-2)*' '), u'\u251B'

If you are trying to run this in cmd.exe, see this question.

如果您尝试在cmd.exe中运行此命令,请参阅此问题。

#1


1  

How about this:

这个怎么样:

x = 3
y = 3

matrix = [[(i+1)+(j*x) for i in range(x)] for j in range(y)]

for row in matrix:
    print ' '.join(map(str, row))

prints:

打印:

1 2 3
4 5 6
7 8 9

EDIT: as suggested in the comments, we can make this last line more flexible to show a proper "matrix" format as follows:

编辑:正如评论中所建议的那样,我们可以使这最后一行更灵活,以显示适当的“矩阵”格式,如下所示:

for row in matrix:
    print ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row))

This means that regardless of the size of x and y, we'll always space our values out evenly. For example if x=6 and y=6, it'll print:

这意味着无论x和y的大小如何,我们都会始终均匀地分配我们的值。例如,如果x = 6且y = 6,它将打印:

 1  2  3  4  5  6
 7  8  9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36

if x=10 and y=10 it'll print:

如果x = 10且y = 10,它将打印:

  1   2   3   4   5   6   7   8   9  10
 11  12  13  14  15  16  17  18  19  20
 21  22  23  24  25  26  27  28  29  30
 31  32  33  34  35  36  37  38  39  40
 41  42  43  44  45  46  47  48  49  50
 51  52  53  54  55  56  57  58  59  60
 61  62  63  64  65  66  67  68  69  70
 71  72  73  74  75  76  77  78  79  80
 81  82  83  84  85  86  87  88  89  90
 91  92  93  94  95  96  97  98  99 100

and so on.

等等。

#2


0  

You can replace your

你可以替换你的

print ""

with

print "",

in order to not add a newline to the end.

为了不在最后添加换行符。

Alternatively, you could use the python 3 print function, which supports this as an argument. To do this, you should import __ future__ and call print like a function. See the code below as an example:

或者,您可以使用python 3 print函数,它支持将此作为参数。为此,您应该导入__ future__并像函数一样调用print。请参阅以下代码作为示例:

from __future__ import print_function
print("my string", end = "")

You can read more about using print as a function on the docs page.

您可以在文档页面上阅读有关将print用作函数的更多信息。

#3


0  

This answer is based on this answer. That answer takes into account only the shape. To make it look like one matrix you can use box-drawing characters.

这个答案是基于这个答案。该答案仅考虑形状。为了使它看起来像一个矩阵,你可以使用盒子绘图字符。

c, x, y = 1, 3, 3

matrix = [[(i+1)+(j*x) for i in range(x)] for j in range(y)]

for row in matrix:
    if c == 1:
        print u'\u250F', ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row)), u'\u2513'
    elif c-y == 0:
        print u'\u2517', ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row)), u'\u251B'
    else:
        print u'\u2503', ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row)), u'\u2503'
    c += 1

Another possibility is:

另一种可能性是:

x, y = 3, 3

matrix = [[(i+1)+(j*x) for i in range(x)] for j in range(y)]

print u'\u250F', ''.join(((2*x)-2)*' '), u'\u2513'
for row in matrix:
    print u'\u2503', ' '.join(s.rjust(len(str(x*y)), ' ') for s in map(str, row)), u'\u2503'
print u'\u2517', ''.join(((2*x)-2)*' '), u'\u251B'

If you are trying to run this in cmd.exe, see this question.

如果您尝试在cmd.exe中运行此命令,请参阅此问题。