Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting list of tuples to decimals and adding them to a file

I have a list of tuples as coordinates (it has to be this way)

points = [(x1, y1), (x2, y2), ...]

to draw a polygon in matplotlib. To get these coordinates I first created an empty list points = [] and then wrote a function to calculate each point from the coordinates of the centre, number of sides, side length and angle of rotation. After the function I wrote a code to read the above initial values from user's input and check their validity, and then call the function if the check is successful.

Now I want to store the coordinates and the number of points in a file as follows:

number of points
x1, y1
x2, y2
...
xn, yn

where each coordinate is written to 3 decimal places. Therefore, I need to format my tuples to 3 decimals, then convert them to strings and then write them in a file, and I want it in the shortest possible way.

I thought I would do something like lines = [float("{:.3f}".format(j)) for j in points] (which doesn't work since I have tuples) and then

lines.insert(0, len(points))
with open('points.txt', 'w') as f:
f.writelines("%s\n" % l for l in lines)

The above solution seems very nice to me, but I can't find a way to do the first line (formatting to decimals) for tuples, so I was wondering how could I possibly format a list of tuples to decimals to store them in a list for the following use of writelines and conversion into strings? Or if there is a shorter and better way of doing this, I would appreciate any hints. Thank you!

like image 719
spfortray Avatar asked Jan 27 '23 03:01

spfortray


1 Answers

You can directly write the floats into your file:

Testdata:

import random

tupledata = [(random.uniform(-5,5),random.uniform(-5,5) ) for _ in range(10)]
print(tupledata)

Output:

[(1.4248082335110652, 1.9169955985773148), (0.7948001195399392, 0.6827204752328884),
 (-0.7506234890561183, 3.5787165366514735), (-1.0180103847958843, 2.260945997153615), 
 (-2.951745273938622, 4.0178333333006435), (1.8200624561140613, -2.048841087823593), 
 (-2.2727453771856765, 1.3697390993773828), (1.3427726323007603, -1.7616141110472583), 
 (0.5022889371913024, 4.88736204694349), (2.345381610723872, -2.2849852099748915)]

Write to formatted:

with open("n.txt","w") as w:
    # w.write(f"{len(tupledata)}\n")  # uncomment for line number on top
    for t in tupledata:                 
        w.write("{:.3f},{:.3f}\n".format(*t)) 

        # for python 3.6 up you can alternatively use string literal interpolation:
        # see    https://www.python.org/dev/peps/pep-0498/

        # w.write(f"{t[0]:.3f},{t[1]:.3f}\n")

with open("n.txt","r") as r:
    print(r.read())

Output in file:

1.425,1.917
0.795,0.683
-0.751,3.579
-1.018,2.261
-2.952,4.018
1.820,-2.049
-2.273,1.370
1.343,-1.762
0.502,4.887
2.345,-2.285

See proper name for python * operator? for what *t does. Hint: print(*[1,2,3]) == print(1,2,3)


  • formatting syntax: format mini language
like image 85
Patrick Artner Avatar answered Jan 29 '23 17:01

Patrick Artner