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 get entries of array with separated by string in one line?

I have this data

[[ 2.696000e+00  0.000000e+00  0.000000e+00  0.000000e+00]
 [ 2.577000e+00  0.000000e+00  0.000000e+00  0.000000e+00]
 [ 0.000000e+00 -2.560096e+03  0.000000e+00  0.000000e+00]
...

and want to print it as such, for each row

2.696000e+00 & 0.000000e+00 & 0.000000e+00 & 0.000000e+00 //
2.577000e+00 & 0.000000e+00 & 0.000000e+00 & 0.000000e+00 //
...

I have done is

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

for k in range(1):
for i in range(len(data[k])):
    print(data[k][i],'&')
    if i == len(data[k])-1:
        print(data[k][i],'//')

Which outputs

2.696 &
0.0 &
0.0 &
0.0 &
0.0 //

How to get rid of the new line between each entry of the row?

>Solution :

You can use str.join to join the numbers by & and then print // as last string:

data = [
    [2.696000e00, 0.000000e00, 0.000000e00, 0.000000e00],
    [2.577000e00, 0.000000e00, 0.000000e00, 0.000000e00],
    [0.000000e00, -2.560096e03, 0.000000e00, 0.000000e00],
]

for row in data:
    print(' & '.join(map(str, row)), '//')

Prints:

2.696 & 0.0 & 0.0 & 0.0 //
2.577 & 0.0 & 0.0 & 0.0 //
0.0 & -2560.096 & 0.0 & 0.0 //
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