3

I'm trying to print an array like: Table = [(0,0,0),(0,0,1),(0,1,0)].

Therefore, I want to print this array as a table like:

0 0 0
0 0 1
0 1 0

How can I make this?

I tried a simple print Table but this prints the array like this:

[(0,0,0),(0,0,1),(0,1,0)]

6 Answers 6

11

Have you tried a basic for loop?

for line in Table:
    print ' '.join(map(str, line))

It's prettier in Python 3:

for line in Table:
    print(*line)
Sign up to request clarification or add additional context in comments.

1 Comment

You also can separate columns by a tab, to make it easier to visualize: for line in Table: print('\t'.join(map(str, line))).
4

In python3 you can print array as a table in one line:

[print(*line) for line in table]

Comments

2

@Tigerhawk's answer is very good if you want something simple.

For other cases, I highly suggest using the tabulate module, allowing great flexibility, and overcoming potential problems like few-digit ints which would cause shifting:

1 2 3
1 12 3
1 2 3

1 Comment

Thanks for the suggestion :)
1

You could use PrettyTable package:

Example for your variable (Table) with three columns:

from prettytable import PrettyTable

def makePrettyTable(table_col1, table_col2, table_col3):
    table = PrettyTable()
    table.add_column("Column-1", table_col1)
    table.add_column("Column-2", table_col2)
    table.add_column("Column-3", table_col3)
    return table

Comments

0
import string
for item in [(0, 0, 0), (0, 0, 1), (0, 1, 0)]:
  temp = (' '.join(str(s) for s in item) + '\n') 
  print(string.replace(temp, '\n', ''))

Test

   $ python test.py 
    0 0 0
    0 0 1
    0 1 0

3 Comments

What is the point of adding a newline and then replacing it? And why import string for that? It's extra running in circles for no reason.
Correct output is the best point. What did you try: Voting and commenting. What a programmer...a voter and a commenter.
@Programmer400 He wrote the accepted answer, that's what he did. And why did you add then remove the newline?
-1

I usually do something like this:

def printMatrix(s):

    for i in range(len(s)):
        for j in range(len(s[0])):
            print("%5d " % (s[i][j]), end="")
        print('\n')  

And this code gives you column and row numbers:

def printMatrix(s):

    # Do heading
    print("     ", end="")
    for j in range(len(s[0])):
        print("%5d " % j, end="")
    print()
    print("     ", end="")
    for j in range(len(s[0])):
        print("------", end="")
    print()
    # Matrix contents
    for i in range(len(s)):
        print("%3d |" % (i), end="") # Row nums
        for j in range(len(s[0])):
            print("%5d " % (s[i][j]), end="")
        print()  

The output looks like this:

         0     1     2 
     ------------------
  0 |    0     0     0 
  1 |    0     0     1 
  2 |    0     1     0 

7 Comments

Python has excellent syntax for iterating over sequences without needing to mess with indices.
@TigerhawkT3, yeah I know this, but I want the indices. I could use enumerate I guess, but it doesn't really add anything.
The OP is using Python 2, by the way, and you're using features that require print() as a function (and I don't see the necessary import). On top of being a reinvention of the wheel (see other answers for modules to print tables in a fancy way), this flat-out won't work.
Did they say they were using Python 2? I don't see that. Your answer is just too basic mate, what about formatting? Numbers with differing numbers of digits etc?
And my earlier comment wasn't about using range(len(...; it was about iterating over indices like an old-style for (int i=0; i++; i<array.length()) {.... There's zero need for that here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.