python - 从元组列表中提取数据

标签 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

等等...

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

除了使用多个循环或字典之外,有没有一种简单的方法可以做到这一点?最快的方法是什么?

干杯, 凯特

最佳答案

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

关于python - 从元组列表中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23487830/

相关文章:

python - 这是nosetests 仅选择无可执行文件.py 的正常行为吗?

PageRank 的 Python 实现

html - 在图像之间放置详细列表。(CSS/Html)

Python 两个列表列表随机播放

Python 列表无法识别数据库表中的值

java - NullPointerException 在 Android 中使用 Scanner 从文件读取?

c++ - 如何在 Windows 下监控/记录对远程文件夹的文件访问?

python - 如何从命令行监控谷歌应用程序引擎?

javascript - 如何通过将 AJAX 中的 <input type ="file"> 数据放入对象中来发送它?

python - 球弹跳物理学,系统似乎获得能量(弹得更高)