To print two equal sized 2-D arrays adjacently in a single .txt file

  • Python
  • Thread starter Apashanka
  • Start date
  • Tags
    Arrays File
In summary, to print two equal sized 2-D arrays adjacently in a single .txt file, you must use the print() function and specify the file parameter as well as use a loop to iterate through the arrays and print their values side by side. This allows for a more organized and efficient way of displaying the arrays in a single output file.
  • #1
Apashanka
429
15
I have two 2-D equal sized arrays (m×n) say a,b and want to print them in a single .txt file and have tried this using numpy
np.savetxt('data.txt',(a,b)) but it fails..,
Can anyone please help in sort out this
Thanks
 
Technology news on Phys.org
  • #2
Apashanka said:
I have two 2-D equal sized arrays (m×n) say a,b and want to print them in a single .txt file and have tried this using numpy
np.savetxt('data.txt',(a,b)) but it fails..,
Can anyone please help in sort out this
Thanks
Can you show us an example for an input and output ?
 
  • #3
Apashanka said:
have tried this using numpy
np.savetxt('data.txt',(a,b)) but it fails..,
How exactly does it fail? Does it give you a Python error message, or does it display the arrays in a format which isn't what you want? An explicit example, and your code, would heip us.
 
  • #4
jtbell said:
How exactly does it fail? Does it give you a Python error message, or does it display the arrays in a format which isn't what you want? An explicit example, and your code, would heip us.
Okay I will post it...
 
  • #5
I would simply write a program to loop through the indices and print them side by side into these column breakdowns:

(i) :: (j) :: A(i,j) :: B(i,j) :: Diff B - A

You could even count the number of nonzero differences if that is what you’re after.
 
  • Like
Likes Apashanka
  • #6
Apashanka said:
Okay I will post it...
We're waiting...
 
  • Like
Likes jedishrfu
  • #7
jtbell said:
How exactly does it fail? Does it give you a Python error message, or does it display the arrays in a format which isn't what you want? An explicit example, and your code, would heip us.
It shows a python error message showing that there is a problem in the syntax .npsavetext('filename.txt',(a,b)) where a and b are two different 2-D arrays...
Sorry for my inconvenience I will post it in the next few days (atmost Friday)
 
  • #8
Mark44 said:
We're waiting...
Sorry for my inconvenience I will definitely post it ...(at most Friday)
 
  • #9
Apashanka said:
It shows a python error message showing that there is a problem in the syntax .npsavetext('filename.txt',(a,b)) where a and b are two different 2-D arrays...
I don't believe there is a numpy (or scipy) function named either npsavetext or .npsavetext. There is a function named savetext(), but it is limited to saving a single one- or two-dimension array to a text file.

Based on the documentation I found (https://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html), here is an example of how to use savetext().
Python:
import numpy as np
a = [1, 2, 3]
np.savetext('filename.txt', a)  # Last 7 parameters are omitted

The savetext() function takes 9 arguments, of which only the first two are required -- the text file that will receive the elements of the array, and the array itself. There is no way to use this function to save the contents to two arrays in one call to the function, but you should be able to call the function twice to save each array in the same file.

When you are using a library like numpy or scipy, and you're getting syntax errors when you call the library function, the very first place you should look for guidance is in the documentation for the library function.
 
Last edited:
  • Like
Likes jedishrfu, sysprog and Apashanka

FAQ: To print two equal sized 2-D arrays adjacently in a single .txt file

How can I print two equal sized 2-D arrays adjacently in a single .txt file?

You can achieve this by first creating a single 2-D array that contains both of the original arrays. Then, use a loop to print each row of this new array into the .txt file, with a delimiter (such as a comma) between the two original arrays.

Can I use any programming language to accomplish this task?

Yes, you can use any programming language that supports 2-D arrays and file input/output. Examples include Java, Python, C++, and many others.

Do the two arrays have to be the same size?

Yes, in order to print them adjacently in a single .txt file, the two arrays must be of equal size. Otherwise, the output will not be in a neat, organized format.

How can I make the output more visually appealing?

You can use formatting techniques such as adding spaces or tabs between the two arrays, or using a different delimiter (such as a vertical bar or a colon) to separate them. You can also add headers or labels to each array to make the output easier to understand.

Is there a limit to the size of the arrays that can be printed in a single .txt file?

The size of the arrays that can be printed in a single .txt file will depend on the memory and storage capacity of your computer. If the arrays are too large, it may cause performance issues or errors. It is recommended to test with smaller arrays first and then gradually increase the size to find the optimal limit for your system.

Similar threads

Replies
13
Views
2K
Replies
2
Views
21K
Replies
6
Views
1K
Replies
9
Views
1K
Replies
8
Views
1K
Replies
8
Views
1K
Replies
2
Views
1K
Replies
2
Views
2K
Back
Top