Question

I have a data file in the format presented below:

2802 [(0.0098799999999999999, 0.00015441000000000001, 44.072000000000003), (0.012030000000000001, 0.00018802000000000001, 43.107999999999997), (0.013849999999999999, 0.00021647999999999999, 42.286000000000001), (0.01546, 0.00024159999999999999, 41.555), (0.016920000000000001, 0.00026435, 40.889000000000003), (0.018259999999999998, 0.00028529, 40.274000000000001)]

2804 [(0.0098799999999999999, 0.00015441000000000001, 56.283999999999999), (0.012030000000000001, 0.00018802000000000001, 55.149999999999999), (0.013849999999999999, 0.00021647999999999999, 54.18), (0.01546, 0.00024159999999999999, 53.313000000000002), (0.016920000000000001, 0.00026435, 52.521000000000001), (0.018259999999999998, 0.00028529, 51.787999999999997)]

2806 [(0.0098799999999999999, 0.00015441000000000001, 68.855999999999995), (0.012030000000000001, 0.00018802000000000001, 67.409000000000006), (0.013849999999999999, 0.00021647999999999999, 66.177000000000007), (0.01546, 0.00024159999999999999, 65.081000000000003), (0.016920000000000001, 0.00026435, 64.081999999999994), (0.018259999999999998, 0.00028529, 63.161000000000001)]

3794 [(0.0098799999999999999, 0.00015441000000000001, 65.441999999999993), (0.012030000000000001, 0.00018802000000000001, 63.987000000000002), (0.013849999999999999, 0.00021647999999999999, 62.749000000000002), (0.01546, 0.00024159999999999999, 61.648000000000003), (0.016920000000000001, 0.00026435, 60.646000000000001), (0.018259999999999998, 0.00028529, 59.722999999999999)]

and I would like to extract them so that the output looks like:

"Coordinates:" 0.0098799999999999999, 0.00015441000000000001
2802,44.072000000000003
2804,56.283999999999999
2806,68.855999999999995
3794,65.441999999999993

"Coordinates:" 0.012030000000000001, 0.00018802000000000001
2802,43.107999999999997
2804,55.149999999999999
2806,67.409000000000006
3794,63.987000000000002

And so on...

which is basically extracting the first 2 values from a tuple as coordinates, then extracting the value which is in the front of the list (for each combination of coordinates) and then extracting the third value from the tuple (for each combination of the coordinates)

Is there an easy way to do it rather than using multiple loops or dictionaries? What would be the fastest approach?

Cheers, Kate

Was it helpful?

Solution

You can use ast.literal_eval and itertools.izip for this:

from ast import literal_eval
from itertools import izip    #For memory efficiency use this over zip

with open('file.txt') as f:

    #Firstly collect the first column from each line in a list
    column_1 = [line.split(None, 1)[0] for line in f if not line.isspace()]
    f.seek(0)  #Move the file pointer to the start of the file

    # Now we need to use the second column from each line and convert it to
    # a Python list using ast.literal_eval.
    # Then we pass each of these lists to izip with `*` to get a transpose
    # of the data. 


    for x in izip(*(literal_eval(line.split(None, 1)[1]) for line in
                                                        f if not line.isspace())):

        #Here x contains items on the same index from each list.

        print '"Coordinates:" {0}, {1}'.format(*x[0][:2])

        for a, b in izip(column_1, x):
            print "{0}, {1}".format(a, b[-1])
        print

Output:

"Coordinates:" 0.00988, 0.00015441
2802, 44.072
2804, 56.284
2806, 68.856
3794, 65.442

"Coordinates:" 0.01203, 0.00018802
2802, 43.108
2804, 55.15
2806, 67.409
3794, 63.987

"Coordinates:" 0.01385, 0.00021648
2802, 42.286
2804, 54.18
2806, 66.177
3794, 62.749

"Coordinates:" 0.01546, 0.0002416
2802, 41.555
2804, 53.313
2806, 65.081
3794, 61.648

"Coordinates:" 0.01692, 0.00026435
2802, 40.889
2804, 52.521
2806, 64.082
3794, 60.646

"Coordinates:" 0.01826, 0.00028529
2802, 40.274
2804, 51.788
2806, 63.161
3794, 59.723

Replace {0} with {0:.15f} wherever you need to display upto 15 decimal places.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top