首页 > 解决方案 > 对位置历史中具有相同地址的位置进行分组,同时保持位置顺序

问题描述

所以我正在制作一个位置跟踪应用程序,并且我正在尝试实现位置历史记录,但是,目前它只是列出了所有位置。我想对相同的位置进行分组,直到位置发生变化。

我试图psuedocode在 python 中制作这个版本,但它没有任何效果。

这是我在python中尝试过的

l = [1,1,1,2,3,4,6,10,15,12,12,12,9,9,8,6,6,4] // this is what im using to test

grouped = []

for i, location in l:

  if(location == l[i+1] || location == [i-1]):
  
    grouped.append([location, location[i+1]])
    
  else:
  
    grouped.append([location])




    
   

// this is what i want the result to be

[[1,1,1],[2],[3],[4],[6],[10],[15],[12,12,12],[9,9],[8],[6,6],[4]]

先感谢您

标签: pythonarrayslistlocationgrouping

解决方案


l = [1,1,1,2,3,4,6,10,15,12,12,12,9,9,8,6,6,4] 
grouped = []
i = 1
intermediate = [l[0]]
while i < len(l):
    if l[i] == l[i-1]:
        intermediate.append(l[i])
    else:
        grouped.append([elem for elem in intermediate])
        intermediate = [l[i]]
    i = i + 1    

grouped.append(intermediate)

我执行了你的代码。它有很多错误。嘿,如果它符合目的,你能检查一下这个代码吗?


推荐阅读