I am trying to generate a position list (from 0 to n) for points (streetlights) based on their distance to other points (street).
The lists look like this :
lightpoint_list = [14, 20, 1, 1, 12, 6]
distance_list = [3.79, 7.91, 9.27, 22.14, 13.91, 1.27]
order_list = [0, 1, 2, 3, 4, 5]
The order in which they appear is important since I have to add this data in a Dataframe and respect its original order. For example :
FID X Y lightpoint_list distance_list order
1 45.33 69.32 14 3.79
2 45.32 69.22 20 7.91
3 45.31 69.20 1 9.27
4 45.28 69.19 1 22.14
5 45.20 69.21 12 13.91
6 45.22 69.31 6 1.27
In this scenario, FID 1 does not necessarely mean that this point is the first on the street, only that it has been entered first. I need to order these points by their order of appearance on the street. To do so, I matched each lightpoint to its closest point on the street by segmenting the line into points. These results are stored in lightpoint_list where each value is the index value of the corresponding segmentend point on the street. We want to generate an order starting from 0 and going to n (in this particular example, 5) based on their order of appearance on the street (i.e lightpoint_list value).
From this, we understand that lightpoint_list value 1 appears before 6, and so on. The distance_list is generated in case 2 lightpoint_list have the same value. If that happens, it means that 2 streetlights are closest to the point 1 (in this case) than any other points on the street. In order to know which point comes first in order of appearance, the distance between the streetlight and the segmented point of the street is computed and stored in distance list.
We understand then that if there is no duplicates in lightpoint_list, we do not need the distance_list and can simply rank the points by ascending order. However, if we have duplicates (like in this case), we need to refer to the distance of these duplicates and sort them by ascending distance.
A result of this would be :
order_list = [4, 5, 0, 1, 3, 2]
that I could append to a Dataframe like this :
FID X Y lightpoint_list distance_list order
1 45.33 69.32 14 3.79 4
2 45.32 69.22 20 7.91 5
3 45.31 69.20 1 9.27 0
4 45.28 69.19 1 22.14 1
5 45.20 69.21 12 13.91 3
6 45.22 69.31 6 1.27 2
答案 0 :(得分:1)
如果我对您的理解正确,那么您想先按lightpoint_list
进行排序,如果亮点相同,则要按distance_list
进行排序。因此解决方案很明确,您可以打包和排序:
lightpoint_list = [14, 20, 1, 1, 12, 6]
distance_list = [3.79, 7.91, 9.27, 22.14, 13.91, 1.27]
order_list = [0, 1, 2, 3, 4, 5]
sorted_data = sorted(zip(lightpoint_list, distance_list, order_list))
print(f"sorted_data={sorted_data}")
for i, data in enumerate(sorted_data):
order_list[data[2]] = i
print(f"order_list={order_list}")
输出:
sorted_data=[(1, 9.27, 2), (1, 22.14, 3), (6, 1.27, 5), (12, 13.91, 4), (14, 3.79, 0), (20, 7.91, 1)]
order_list=[4, 5, 0, 1, 3, 2]
希望对您有所帮助,如果还有其他问题,请发表评论。 :)