Pad each element of the list with the following 0s

  iteration, nested-lists, python-3.x

I have a list of lists:

0      [[[20.5973832, 52.8685205], [20.5974081, 52.86...
1      [[[21.0072715, 52.2413049], [21.0072603, 52.24...
2      [[[18.8446673, 50.4508418], [18.8447041, 50.45...
3      [[[18.8649393, 50.4483321], [18.8649802, 50.44...
4      [[[16.7529018, 53.1424612], [16.7528965, 53..

I need to iterate over each element (each coordinate number) of the list, and make sure it has 7 digits after the period. If it doesn’t, I need to pad it with a 0 at the end to make it to have 7 digits after the period.

Each number is a float, but I can convert it to string to use the len() function.

The code I have is:

for a in list_of_lists:
    for b in a:
        for c in b:
            for d in c:
                if(len(str(d))<10):
                    d = str(d).ljust(10-len(str(d)), '0')

The error I am getting is:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-46-70d9d844f41b> in <module>
      3 for a in list_of_lists:
      4     for b in a:
----> 5         for c in b:
      6             for d in c:
      7                 if(len(str(d))<10):

TypeError: 'float' object is not iterable

What is the better way of achieving this?

Source: Python-3x Questions

LEAVE A COMMENT