Gia*_*ear 1 python file-io list-comprehension
我有一个简单的问题,对不起,如果我在stackoverflow中发布.我是python的新手,我不记得我怎么能在列表压缩ax,y,z中读取
我的文件是ax,y,z文件,其中每一行是一个点:
x1,y1,z1
x2,y2,z2
x3,y3,z3
........
inFile = "Myfile.las"
with lasfile.File(inFile, None, 'r') as f:
# missing part
points =[]
Run Code Online (Sandbox Code Playgroud)
我希望用x和y保存一个对象
提前致谢并抱歉这个简单的问题
你想要一个x和y坐标列表,这很容易:
with lasfile.File(inFile, None, 'r') as f:
# missing part
points = [line.split(',')[:2] for line in lasfile]
Run Code Online (Sandbox Code Playgroud)
如果这些坐标是整数,您可以通过快速调用将它们转换为python int(来自str)map():
points = [map(int, line.split(',')[:2]) for line in lasfile]
Run Code Online (Sandbox Code Playgroud)
在python 3中,where map是一个生成器,最好使用嵌套列表理解:
points = [[int(i) for i in line.split(',')[:2]] for line in lasfile]
Run Code Online (Sandbox Code Playgroud)
这将导致列表列表:
[[x1, y1], [x2, y2], ...]
Run Code Online (Sandbox Code Playgroud)