formatting strings in list of lists
Question: I have this list of list

    
    
    a = [['c', '1.3e-8', '4.5e-8'], ['h', '3.4e-5', '2.3e-7', '2.3e-5']]
    

I want to format the 'e' strings only as

    
    
    a = [[ 'c', '0.000000013', '0.000000045'], ['h', '0.000034', '0.00000023', '0.000023']]
    

How can I do this in Python? Thanks!


Answer: If you sure only first element is not a float.

    
    
    from decimal import Decimal
    
    a = [['c', '1.3e-8', '4.5e-8'], ['h', '3.4e-5', '2.3e-7', '2.3e-5']]
    
    for inx, rec in enumerate(a):
        a[inx] = [rec[0]] + ['{:.{precise}f}'.format(Decimal(val),
                    precise=int(val[-1])+1) for val in rec[1:]]
    
    print(a)
    

Output:

    
    
    [['c', '0.000000013', '0.000000045'], ['h', '0.000034', '0.00000023', '0.000023']]
    

