从元组列表中提取数据

时间:2014-05-06 06:53:27

标签: python list file tuples

我有一个数据文件,格式如下:

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)]

我想提取它们,以便输出如下:

"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

等等......

基本上是从元组中提取前2个值作为坐标,然后提取列表前面的值(对于每个坐标组合),然后从元组中提取第三个值(对于每个组合坐标)

有没有一种简单的方法可以做到而不是使用多个循环或词典?什么是最快的方法?

干杯, 凯特

1 个答案:

答案 0 :(得分:2)

您可以使用ast.literal_evalitertools.izip

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

<强>输出:

"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

{0}替换为{0:.15f},只要您需要显示最多15个小数位。