0

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.

6
  • 1
    I don't see any double spaces in s before you do the replace. Commented Oct 4, 2018 at 12:50
  • 1
    There's no double spaces, why do you think it doesn't line up where a 0 is omitted? Commented Oct 4, 2018 at 12:50
  • actually, it won't be seen in here, but when I am catching it a string on client side, I get to see 2 spaces. I just want to eradicate it with 0 appended in the last of the values where there are less values.. Is it doable? Commented Oct 4, 2018 at 12:52
  • 1
    Why convert the list of list of floats to string? Why not just flatten and then join? Commented Oct 4, 2018 at 12:54
  • You can use itertools.chain.from_iterable to flatten the input list, and then str.format to print each number with the precision that you desire. Commented Oct 4, 2018 at 12:56

2 Answers 2

3

I guess you want to accomplish something like this:

>>> result = "\n".join([" ".join([format(n, "0.08f") for n in line]) for line in d])
>>> print(result)
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.42992980 0.43789476 0.42854127
0.42436373 0.42762490 0.43576592 0.42636240

Change the format string ("0.08f") to suit your needs.

Sign up to request clarification or add additional context in comments.

1 Comment

Let me try it sir. I guess this will help.
1

Try with a simple formatting .8f

>>> 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]]
>>> dFormatted = '\n'.join(' '.join(f'{num:.8f}' for num in nums) for nums in d)
>>> print(dFormatted)
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.42992980 0.43789476 0.42854127
0.42436373 0.42762490 0.43576592 0.42636240
>>>

2 Comments

f strings (PEP-498) are nice and even more concise than the format function or the string.format method but only available in 3.6 IINM.
@PauloScardine yes, you can do it with '{num.8f}'.format(num=num), '{.8f}'.format(num), '%.8f' % num and with format() like in your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.