Here is what I tried and what I am trying to achieve:
>>> d = [[0.4246955,0.42829293,0.43621248,0.42680067],
... [0.42453277,0.42806646,0.43601942,0.42658913],
... [0.42681128,0.43040696,0.43824464,0.42888936],
... [0.42648485,0.4299298,0.43789476,0.42854127],
... [0.42436373,0.4276249,0.43576592,0.4263624]]
>>> d
[[0.4246955, 0.42829293, 0.43621248, 0.42680067], [0.42453277, 0.42806646, 0.43601942, 0.42658913], [0.42681128, 0.43040696, 0.43824464, 0.42888936], [0.42648485, 0.4299298, 0.43789476, 0.42854127], [0.42436373, 0.4276249, 0.43576592, 0.4263624]]
>>> print(d)
[[0.4246955, 0.42829293, 0.43621248, 0.42680067], [0.42453277, 0.42806646, 0.43601942, 0.42658913], [0.42681128, 0.43040696, 0.43824464, 0.42888936], [0.42648485, 0.4299298, 0.43789476, 0.42854127], [0.42436373, 0.4276249, 0.43576592, 0.4263624]]
>>> s = ""
>>> for i in d:
... s = s+str(i).replace("[","").replace("]","").replace(",","").strip()+"\n"
...
>>> s
'0.4246955 0.42829293 0.43621248 0.42680067\n0.42453277 0.42806646 0.43601942 0.42658913\n0.42681128 0.43040696 0.43824464 0.42888936\n0.42648485 0.4299298 0.43789476 0.42854127\n0.42436373 0.4276249 0.43576592 0.4263624\n'
>>> print(s)
0.4246955 0.42829293 0.43621248 0.42680067
0.42453277 0.42806646 0.43601942 0.42658913
0.42681128 0.43040696 0.43824464 0.42888936
0.42648485 0.4299298 0.43789476 0.42854127
0.42436373 0.4276249 0.43576592 0.4263624
>>> s = s.replace(" ","0 ")
>>> print(s)
0.4246955 0.42829293 0.43621248 0.42680067
0.42453277 0.42806646 0.43601942 0.42658913
0.42681128 0.43040696 0.43824464 0.42888936
0.42648485 0.4299298 0.43789476 0.42854127
0.42436373 0.4276249 0.43576592 0.4263624
As one can see that the each element is not of the same length. Hence, while converting to string I am getting double spaces, which disturbing my splitting process in another program.
I wanted to know how I can replace the double spaces with single space or with 0 and space as I tried in the above code.
For example see the following:
The string is:
0.4246955 0.42829293 0.43621248 0.42680067
0.42453277 0.42806646 0.43601942 0.42658913
0.42681128 0.43040696 0.43824464 0.42888936
0.42648485 0.4299298 0.43789476 0.42854127
0.42436373 0.4276249 0.43576592 0.4263624
, so I want it to become like this:
0.42469550 0.42829293 0.43621248 0.42680067
0.42453277 0.42806646 0.43601942 0.42658913
0.42681128 0.43040696 0.43824464 0.42888936
0.42648485 0.4299298 0.43789476 0.428541270
0.42436373 0.4276249 0.43576592 0.426362400
Please let me know what I can do.
sbefore you do the replace.itertools.chain.from_iterableto flatten the input list, and thenstr.formatto print each number with the precision that you desire.