Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert a NumPy array to a string with the same format it can be set with?

I have a NumPy array with a shape of (100,3). If I try to print the array, it looks like this:

[[ 0  8 10]
[ 1 29 10]
[ 3 29  6]
...
[ 2  9  5]
[ 2 10  5]
[ 2 26 10]]

I want to convert the array to a string, but with this format:

(0,8,10),(1,29,10),(3,29,6)...

That way I could set the array like so:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

nparray = np.array([InsertStringHere])

How would I do this?

>Solution :

Try this:

import numpy as np

array = np.array([[0, 8, 10],
                  [1, 29, 10],
                  [3, 29, 6],
                  [2, 9, 5],
                  [2, 10, 5],
                  [2, 26, 10]])

string_array = ",".join([str(tuple(row)) for row in array])

print(string_array)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading