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 6016345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:58:09+00:00 2026-05-23T02:58:09+00:00

I have a small question. I have a file in the following format: 1

  • 0

I have a small question. I have a file in the following format:

1 2 
1 2 3
1 2
1 2 3 4
2 4

The values in the code actually represent numbers(not necessarily single digit), but they can be any number, can be floating point values too.

Input file: For a particular row, each number is separated from another by a single whitespace (the separator can’t be anything other than whitespace).

My task: I want to zero fill the empty spaces in such a way that it looks like this, i.e. fill up the empty spaces in such a way that it gives me a nice matrix-looking format:

1 2 0 0
1 2 3 0
1 2 0 0
1 2 3 4
2 4 0 0

Output file: Same rule applies. For a particular row, each number is separated from another by a single whitespace only.

Language used: Python(or may be Shell, if that’s possible)

I know there is this function called zfill, but I don’t think that would be much of a help to me.

My solution: Find the (maximum length/2) of each line, using len and max functions. Then, using split(), fill up with zeros at appropriate places of each line. I am afraid it might turn into a dirty code and I’m sure there are better ways to accomplish this task.

Any suggestion is welcome.

Thank you!

  • 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-23T02:58:10+00:00Added an answer on May 23, 2026 at 2:58 am

    Assume myfile is the open file. We use izip_longest from itertools to iterate over the columns of the input file, filling in "0" for missing values:

    [('1', '1', '1', '1', '2'),  ('2', '2', '2', '2', '4'),      
     ('0', '3', '0', '3', '0'), ('0', '0', '0', '4', '0')]
    

    Then we simply zip this output again to restore the rows with zeroes filled in. This is the code:

    from itertools import izip_longest
    
    rows = [line.split() for line in myfile]            # Read
    rows = zip(*izip_longest(*rows, fillvalue="0"))     # Add zeroes
    print "\n".join(" ".join(row) for row in rows)      # Write
    

    EDIT: The above (imho elegant) solution is slightly slower (8.55 usec vs. 7.08 usec) than the naive approach:

    rows = [line.split() for line in myfile]
    maxlen = max(len(x) for x in rows)
    for row in rows:
        print " ".join(row + ["0"] * (maxlen - len(row)))
    

    Re: comment

    If you want to align the columns it’s easiest to amend the first approach, because there we already have the numbers arranged by column at one point. That makes finding the column width easy.

    from itertools import izip_longest
    
    rows = [line.split() for line in myfile]
    columns = list(izip_longest(*rows, fillvalue="0"))
    column_width = [max(len(num) for num in col) for col in columns]
    
    # We make a template of the form "{0:>a} {1:>b} {2:>c} ...",
    # where a, b, c, ... are the column widths:
    column_template = "{{{0}:>{1}s}}"
    row_template = " ".join(column_template.format(i, n) for
        i, n in enumerate(column_width))
    
    print "\n".join(row_template.format(*row) for row in zip(*columns))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not much into COM interfaces, so i have a small question, say I
I have a small dummy project in VS 2008, contains only the following code
EDIT: Optimization results at end of this question! hi, i have a following code
Greetings, I have the following question. I did not find the exact answer for
I have a small question. I'm trying to create templates for getters for my
I have a small question...How to search through the sub views of navigation bar
I have a small question in passing two or more sys.argv arguments in Python.
I have a small question. I have to pass an argument to my measurement
I have a small question hoping someone will help me. Is there any way
I have a small question about the Core Bluetooth framework. I know Core Bluetooth

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.