1

Given an array of array A defined as

A = [[1, 2, 3, 4], [10, 20, 30, 40], [100, 200, 300, 400]],

if print function is called

for i in range(0,3):
    print A[i]

the following is the output

[1, 2, 3, 4]
[10, 20, 30, 40]
[100, 200, 300, 400].

How can I get a "prettier" output like this:

[  1,   2,   3,   4]
[ 10,  20,  30,  40]
[100, 200, 300, 400]

??? Thank you

5 Answers 5

6

All you need to know is the maximum number of digits that there could be. If the number of digits is three as in your example, do this:

for i in A:
    print(", ".join([str(l).rjust(3) for l in i]))

Using str(i).rjust(3) puts i right-justified in a field of width 3 where the extra characters are spaces. You could make them zeros with str(i).zfill(3), or you could make them anything you want with str(i).rjust(3, "&") for example.

Output:

  1,   2,   3,   4
 10,  20,  30,  40
100, 200, 300, 400

Of course, to make it applicable for more situations, you could use len(str(max(map(max, A)))) instead of hardcoding the 3.

Sign up to request clarification or add additional context in comments.

2 Comments

wow! just learned something new today....i came up with a solution but it was very un-pythonic. Thanks @zondo
this won't work as nicely dynamically, and dynamic is what python has to be for me, so it should be more applicable to many scenarios rather than suited for one situation.
2

This code will more dynamic. This will find the maximum number's length and rjust by max_len.

A = [[1, 2, 3, 4], [10, 20, 30, 40], [11100, 20033, 300, 400]]

max_len = len(str(max( max(i) for i in A)))

for i in A:
    print(", ".join([str(l).rjust(max_len) for l in i]))

Comments

0

Why not using a numpy array for this ?

import numpy as np
print np.array(A)

[[  1   2   3   4]
 [ 10  20  30  40]
 [100 200 300 400]]

Comments

0

You can't have those spaces in there if the record is an integer, but this code seems to split up the array nicely.

A = [1999999, 2, 3, 47678], [10, 20, 30, 40], [100, 200, 300, 400]
MaxLength=0
for i in range(0,3):
    for x in A[i]:
        MaxLength =len(str(x)) if MaxLength<len(str(x)) else MaxLength
for i in range(0,3):
    for x in range(0,len(A[i])):
        Length=MaxLength-len(str(A[i][x]))
        print((" "*Length)+str(A[i][x]),end="|")
    print()

If you do want, you can call this up in a definition, just do this:

def TableFormat(A):
    MaxLength=0
    for i in range(0,3):
        for x in A[i]:
            MaxLength =len(str(x)) if MaxLength<len(str(x)) else MaxLength
    for i in range(0,3):
        for x in range(0,len(A[i])):
            Length=MaxLength-len(str(A[i][x]))
            print((" "*Length)+str(A[i][x]),end="|")
        print()

Then you can print the table neatly by doing TableFormat(A) with A as the array. The end="|" can be swapped for anything you want to divide the records with

Comments

0

Easiest way is to use two for loops

    for list in A:
        for element in list:
            print element, '\t',
        print '\n'

Try this.

Comments

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.