Print a two dimension list with format in Python

打印一二维列表格式在Python

Tags: python
标签: python

问题 (Question)

So I have defined a two dimension list in python using:

column = 3
row = 2
Matrix =  [['' for i in range(column)] for j in range(row)]

Then I started adding values to it:

Matrix[0][0] += 'A'
Matrix[1][0] += 'AB'
Matrix[2][0] += 'ABC'
Matrix[0][1] += 'X'
Matrix[1][1] += 'XY'
Matrix[2][1] += 'XYZ'

Then I started printing with hope of some sort of format:

for i in range(0, row, 1):
    for j in range(0, column, 1):
        print(Matrix[i][j] + '\t')

I was thinking of get result like

A   AB   ABC
X   XY   XYZ

But actually I got:

A   
AB  
ABC     
X   
XY  
XYZ 

Just wondering what is wrong with my code...

所以我所定义的一二个维度在Python中使用列表:

column = 3
row = 2
Matrix =  [['' for i in range(column)] for j in range(row)]

然后我开始增加其价值:

Matrix[0][0] += 'A'
Matrix[1][0] += 'AB'
Matrix[2][0] += 'ABC'
Matrix[0][1] += 'X'
Matrix[1][1] += 'XY'
Matrix[2][1] += 'XYZ'

然后我开始与一些希望打印格式:

for i in range(0, row, 1):
    for j in range(0, column, 1):
        print(Matrix[i][j] + '\t')

我想得到的结果一样,

A   AB   ABC
X   XY   XYZ

但实际上我了:

A   
AB  
ABC     
X   
XY  
XYZ 

只是想知道什么是我的代码错误…

最佳答案 (Best Answer)

You only want a new line each i, and not each j. Generally they're implicit, so you need to specify no newline:

Python 3:

for i in range(0, row, 1):
    for j in range(0, column, 1):
        print(Matrix[i][j] + '\t', end="")  # <-- end="" means no newline
    print('')  # <-- implicit newline, only in row loop

Python 2:

for i in range(0, row, 1):
    for j in range(0, column, 1):
        print Matrix[i][j] + '\t',  # <-- comma at the end means no newline
    print('')  # <-- implicit newline, only in row loop

你只需要一个新的线i,和每个j。他们一般都是内隐的,所以你需要指定不换行:

Python 3:

for i in range(0, row, 1):
    for j in range(0, column, 1):
        print(Matrix[i][j] + '\t', end="")  # <-- end="" means no newline
    print('')  # <-- implicit newline, only in row loop

Python 2:

for i in range(0, row, 1):
    for j in range(0, column, 1):
        print Matrix[i][j] + '\t',  # <-- comma at the end means no newline
    print('')  # <-- implicit newline, only in row loop

答案 (Answer) 2

the print function adds a newline to the end.

a way to signal that you dont want a new line is to use add a comma at the end

Python 3

print(Matrix[i][j],"\t",)

Python 2.7

print Matrix[i][j],"\t",

print功能添加一个换行符结束。

一种信号:你想要一个新的线是用在末尾添加一个逗号

Python 3

print(Matrix[i][j],"\t",)

Python 2.7

print Matrix[i][j],"\t",

答案 (Answer) 3

You can use the sep argument in python3:

for row in zip(*Matrix):
    print(*row, sep='\t')

You have the rows as columns in your matrix, so you'll need to zip it to get at the rows first.
Then, you can print the individual elements in the row, with a TAB between them

In python2, this would be:

import itertools for row in itertools.izip(*Matrix): print('\t'.join(row))

你可以使用sep在python3争论:

for row in zip(*Matrix):
    print(*row, sep='\t')

你有在你的矩阵的列的行,所以你需要压缩到一排排第一。
然后,你可以在一行打印单个元素,与标签他们之间

在python2,这将是:

进口itertools 行在itertools iZip应用。(×矩阵):打印('\t'加入(行)。

答案 (Answer) 4

First you may want to check how you added your values. While iterating over the row, you will receive an 'IndexError: list out of range'

Adding your values should look like this:

Matrix[0][0] = 'A'
Matrix[0][1] = 'AB'
Matrix[0][2] = 'ABC'
Matrix[1][0] = 'X'
Matrix[1][1] = 'XY'
Matrix[1][2] = 'XYZ'

After that, then all you have to do is iterate over your rows using the method join() on each row.

for i in range(row):
  print '\t'.join(Matrix[i])

That will print your desired result:

A   AB  ABC
X   XY  XYZ

首先你可能要检查你如何增加你的价值。而重复行,您将收到一个“IndexError: list out of range'

增加你的价值观应该是这样的:

Matrix[0][0] = 'A'
Matrix[0][1] = 'AB'
Matrix[0][2] = 'ABC'
Matrix[1][0] = 'X'
Matrix[1][1] = 'XY'
Matrix[1][2] = 'XYZ'

在那之后,然后你要做的就是遍历你的行的使用方法join()在每一行。

for i in range(row):
  print '\t'.join(Matrix[i])

将打印你期望的结果:

A   AB  ABC
X   XY  XYZ

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/22080628