1

I have a simple question and sorry if i post in stackoverflow. I am quite new in python and i don't remember how i can read in list compression a x,y,z

my file is a x,y,z file where each line is a points:

x1,y1,z1
x2,y2,z2
x3,y3,z3
........

inFile = "Myfile.las"

with lasfile.File(inFile, None, 'r') as f:
     # missing part
     points =[]

what i wish to save an object with only x and y

Thanks in advance and sorry for the simple question

Bryan
  • 17,112
  • 7
  • 57
  • 80
Gianni Spear
  • 7,033
  • 22
  • 82
  • 131

1 Answers1

8

You you wanted a list of x and y coordinates, it's easy enough:

with lasfile.File(inFile, None, 'r') as f:
     # missing part
     points = [line.split(',')[:2] for line in lasfile]

If these coordinates are integers, you can convert them to python int (from str) with a quick call to map():

points = [map(int, line.split(',')[:2]) for line in lasfile]

In python 3, where map is a generator, it's probably best to use a nested list comprehension:

points = [[int(i) for i in line.split(',')[:2]] for line in lasfile]

This'll result in a list of lists:

[[x1, y1], [x2, y2], ...]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Or a list of iterables in python3.x :-/ -- Sometimes I wish they had kept a map-like function which returns a list. `lmap` or something ... – mgilson Oct 09 '12 at 12:14
  • @mgilson: Indeed; in which case a nested list comprehension would probably be the more readable. – Martijn Pieters Oct 09 '12 at 12:15
  • thanks my idea is reading the x and y and find Xmax, Xmin, Ymax and Ymin. I wrote a code but is really not elegant. – Gianni Spear Oct 09 '12 at 12:57
  • using your code i have this problem Traceback (most recent call last): File "", line 1, in AttributeError: __exit__ – Gianni Spear Oct 09 '12 at 13:07
  • @Gianni: `lasfile.File` is not a context manager then and cannot be used in a `with` statement; this has nothing to do with my code, your question also uses `with`. – Martijn Pieters Oct 09 '12 at 13:10
  • yes true. Yesterday with lasfile.File(inFile, None, 'r') as f: ... inside_points = [p for p in f if pnpoly(p.x, p.y, verts)] ... see http://stackoverflow.com/questions/12769353/python-suggestions-to-improve-a-chunk-by-chunk-code-to-read-several-millions-of was working and today no. I cannot explain what's happend – Gianni Spear Oct 09 '12 at 13:19
  • @Gianni: From what I can see from your questions, `lasfile.File()` is breaking all *sorts* of python conventions.. – Martijn Pieters Oct 09 '12 at 13:22
  • 1
    @Gianni: glancing over that [project's codebase](https://github.com/libLAS/libLAS/blob/master/python/liblas/file.py) I see no support for using `File` objects as context managers *at all*. You *always* should get an attribute error on `__exit__`. – Martijn Pieters Oct 09 '12 at 13:27
  • Hey Martijin i resolved like this: f = lasfile.File(inFile,None,'r') points = [(p.x,p.y) for p in f] X_Max = max(zip(*points)[0]) X_Min = min(zip(*points)[0]) Y_Max = max(zip(*points)[1]) Y_Min = min(zip(*points)[1]) in order to get the X min and max and the Y min and max – Gianni Spear Oct 09 '12 at 13:33
  • @Gianni: Please don't edit my answer; your problem was with how to do the list comprehension, not how to handle your file object. – Martijn Pieters Oct 09 '12 at 13:38
  • @PoweRoy: That edit should have been rejected, not 'improved'. – Martijn Pieters Oct 09 '12 at 13:39
  • @MartijnPieters: really sorry i am new of stackover. First and last time i do. Sorry again – Gianni Spear Oct 09 '12 at 13:40
  • @MartijnPieters: sorry missed that someone else made the addition. Thought it was the one who posted. – RvdK Oct 09 '12 at 13:44
  • @PoweRoy: Edits to your own posts *never* need approval. *All* edits in the review queue are by someone editing a post that is not their own. – Martijn Pieters Oct 09 '12 at 13:46