【问题标题】:Extract data from a list of tuples从元组列表中提取数据
【发布时间】:2014-05-06 06:53:26
【问题描述】:

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

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

等等……

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

有没有比使用多个循环或字典更简单的方法?最快的方法是什么?

干杯, 凯特

【问题讨论】:

  • 请以有效的 Python 数据结构形式发布数据。
  • @200OK 也许这是某个文件的内容,而不是 Python 数据结构。
  • 大家好,这是我文件的内容
  • 数据如何流动并不明显。需要解释。
  • 我编辑了问题,希望现在更清楚

标签: python list file tuples


【解决方案1】:

您可以为此使用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

在需要显示最多 15 位小数的地方将 {0} 替换为 {0:.15f}

【讨论】:

  • 谢谢,但我不断收到错误消息: print '"Coordinates:" {}, {}'.format(*x[0][:2]) ValueError: zero length field name格式
  • @kate88 在 Python 2.6 上,您还需要指定位置:{0}, {1}
  • 谢谢!它完美无缺!也谢谢你的解释!
猜你喜欢
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多