Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
811 views
in Technique[技术] by (71.8m points)

python - How to divide list according to index position?

I have a combined list of lat, lon and alt in a sequence as shown below, now I want to divide the list into 3 parts 1st for lat, 2nd for lon and third for alt.

coords = [48.92504247289378, 9.147973368734435, 29707, 48.92504291087322, 9.147998449546572, 29707, 48.9250463088055, 9.148013780873235, 29707, 48.92505484289239, 9.148021595289876, 29707, 48.925070689333246, 9.148024125370592, 29707]

#list format = [lat,lon,alt,lat,lon,alt.....]

excepted output:

lat = [48.92504247289378,48.92504291087322,48.9250463088055....]
lon = [9.147973368734435,9.147998449546572, 9.148013780873235...]
alt = [29707,29707,29707,...]

I tried:

Split a python list into other "sublists" i.e smaller lists

Python: Split a list into sub-lists based on index ranges

Split a list into parts based on a set of indexes in Python

And a few other solutions, but none of them worked.

Can anyone help me to get the expected output?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just slicing the list would work:

coords = [48.92504247289378, 9.147973368734435, 29707, 48.92504291087322, 9.147998449546572, 29707, 48.9250463088055, 9.148013780873235, 29707, 48.92505484289239, 9.148021595289876, 29707, 48.925070689333246, 9.148024125370592, 29707]
lat = coords[::3]
lon = coords[1::3]
alt = coords[2::3]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...