X_Max = float('-inf')
X_Min = float('+inf')
Y_Max = float('-inf')
Y_Min = float('+inf')
for p in lasfile.File(inFile,None,'r'):
X_Max = max(X_Max, p.x)
X_Min = min(X_Min, p.x)
Y_Max = max(Y_Max, p.y)
Y_Min = min(Y_Min, p.y)
This way you only loop once over your file, as well as avoiding having more than one point in memory at a time.
EDIT File()
is providing a iterator, which is only reading one line at a time from the file and supplying it to the loop variable p
, as and when required.
In your question you used square brackets around your initial points assignment. This is a list comprehension which, as the name suggests, creates a list - so all points are held in memory from that point on. If you used parentheses instead like this:
points = ((p.x,p.y) for p in lasfile.File(inFile,None,'r'))
X_Max = float('-inf')
X_Min = float('+inf')
Y_Max = float('-inf')
Y_Min = float('+inf')
for p in points:
X_Max = max(X_Max, p.x)
X_Min = min(X_Min, p.x)
Y_Max = max(Y_Max, p.y)
Y_Min = min(Y_Min, p.y)
...then Python doesn't create a list, but a generator/iterator - which would return one point at a time until the file is exhausted. This would avoid having all points in memory at the same time - but can only be iterated over once.
For the sake of simplicity though, I've dropped the creation of an additional iterator in favour of just using the lasfile.File()
one directly.