Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3974366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:33:35+00:00 2026-05-20T04:33:35+00:00

Is there any elegant way to exploit the correct spacing feature of print numpy.array

  • 0

Is there any elegant way to exploit the correct spacing feature of print numpy.array to get a 2D array, with proper labels, that aligns properly? For example, given an array with 4 rows and 5 columns, how can I provide the array and appropriately sized lists corresponding to the row and header columns to generate some output that looks like this?

      A   B   C   D   E
Z [[ 85  86  87  88  89]
Y  [ 90 191 192  93  94]
X  [ 95  96  97  98  99]
W  [100 101 102 103 104]]

If I naively try:

import numpy
x = numpy.array([[85, 86, 87, 88, 89], \
                 [90, 191, 192, 93, 94], \
                 [95, 96, 97, 98, 99], \
                 [100,101,102,103,104]])

row_labels = ['Z', 'Y', 'X', 'W']


print "     A   B   C   D   E"
for row, row_index in enumerate(x):
    print row_labels[row_index], row

I get:

      A   B   C   D   E
Z  [85  86  87  88  89]
Y  [90 191 192  93  94]
X  [95  96  97  98  99]
W  [100 101 102 103 104]

Is there any way i can get things to line up intelligently? I am definitely open to using any other library if there is a better way to solve my problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:33:36+00:00Added an answer on May 20, 2026 at 4:33 am

    Assuming all matrix numbers have at most 3 digits, you could replace the last part with this:

    print "     A   B   C   D   E"
    for row_label, row in zip(row_labels, x):
        print '%s [%s]' % (row_label, ' '.join('%03s' % i for i in row))
    

    Which outputs:

         A   B   C   D   E
    Z [ 85  86  87  88  89]
    Y [ 90 191 192  93  94]
    X [ 95  96  97  98  99]
    W [100 101 102 103 104]
    

    Formatting with '%03s' results in a string of length 3 with left padding (using spaces). Use '%04s' for length 4 and so on. The full format string syntax is explained in the Python documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any elegant way of turning [$(div), $(span), $(li)] into $(div, span, li)
Is there any elegant way of splitting a list/dict into two lists/dicts in python,
In java is there any elegant way to compare two different StringBuilders and copy
I have a double[][] array, and I want to get one row into a
I have an abstract class A with 1 method called public int get(int x,
I am interested in keeping a running history of every change which has happened
I am basically reading a text file into a List and I am copying
I'm facing an issue embedding flash content in a Ruby on Rails 3 view
I have background NSInvocationOperation creating and saving NSArray to the NSManagedObject subclass. I know
I use ubuntu 10.04, but have discovered this on other ubuntu distributions as well.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.