提取Python的最小和最大x值

use*_*142 0 python

我编写了一个函数,它将一个带有x,y坐标的文件作为输入,并简单地显示python中的坐标.我想用坐标更多地工作,这是我的问题:

例如,在阅读文件后,我得到:

32, 48.6
36, 49.0
30, 44.1
44, 60.1
46, 57.7
Run Code Online (Sandbox Code Playgroud)

我想提取最小和最大x值.

我读取文件的功能如下:

def readfile(pathname):
    f = open(sti + '/testdata.txt')
    for line in f.readlines():
        line = line.strip()
        x, y = line.split(',')
        x, y= float(x),float(y)
        print line
Run Code Online (Sandbox Code Playgroud)

我正在考虑使用min()和max()创建一个新函数,但是因为我对python很新,我有点卡住了.

如果我例如调用min(readfile(pathname))它只是再次读取整个文件..

任何提示都非常感谢:)

Joh*_*ooy 5

from operator import itemgetter

# replace the readfile function with this list comprehension
points = [map(float, r.split(",")) for r in open(sti + '/testdata.txt')]

# This gets the point at the maximum x/y values
point_max_x = max(points, key=itemgetter(0))
point_max_y = max(points, key=itemgetter(1))

# This just gets the maximum x/y value
max(x for x,y in points)
max(y for x,y in points)
Run Code Online (Sandbox Code Playgroud)

最小值通过更换发现maxmin