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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:00:21+00:00 2026-06-05T06:00:21+00:00

I am writing a multidimensional array data to a text file. I am doing

  • 0

I am writing a multidimensional array data to a text file. I am doing this row wise. The size of the file keeps growing. what techniques should I follow to get the least possible size for the output file?

  • 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-06-05T06:00:22+00:00Added an answer on June 5, 2026 at 6:00 am

    If your array has many zeros you can use sparse matrix representation: instead of writing the whole matrix to the file, only write the nonzero elements (of course, you need to write each element with its indices, one by one). Suppose you want to write this matrix:

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

    You can write this to the file:

    0 3 2
    1 0 1
    3 2 3
    

    In each line, the first number is the row, the second is the column and the third is the stored value.

    If you are writing the file as text, you can switch to binary format: when you write text, you will use a byte for each digit; in binary, you use fixed amount of bytes per number, and wouldn’t have to represent spaces and newlines:

    Writing the numbers 100 200 300 to a file takes 11 bytes if you use text format. But they may be written using 6 bytes if you write three 16-bit integers. In Python, use “wb” and “rb” modes for opening binary files, then write them as bytes:

    f = open('file', 'wb')
    f.write('%c' % 123)
    f.close()
    

    Or — more efficiently,

    import array
    f = open('file', 'wb')
    data = array.array('B')
    data.append(1)
    data.append(2)
    data.append(3)
    data.tofile(f)
    f.close()
    

    Otherwise, then you should probably try compressing the data structure, using standard techniques. Since you tagged your question with python, you will probably be interested in these Python libraries for data compression

    There is also this nice introduction to data compression, a bit heavy on the theoretical side, in case you would like to know more about it.

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

Sidebar

Related Questions

I'm writing a recursive function to construct a multidimensional array. Basically, the problem is
I am writing a script which must copy some names into a multidimensional array,
Writing a function I must declare input and output data types like this: int
I'm writing a simple test program to pass multidimensional arrays. I've been struggling to
Writing a python program, and I came up with this error while using the
I'm trying to allocate memory for a multidimensional array (8 rows, 3 columns). Here's
I need to iterate over an array of arbitrary rank. This is for both
Writing a SQL query that should return the average charges for a preceding 91
Writing a function to generate and push some random data inside of the unknown
writing in QT and QErrorMessage by default has checkbox saying: Show this message again

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.