python - 对位置历史中具有相同地址的位置进行分组,同时保持位置顺序
问题描述
所以我正在制作一个位置跟踪应用程序,并且我正在尝试实现位置历史记录,但是,目前它只是列出了所有位置。我想对相同的位置进行分组,直到位置发生变化。
我试图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]]
先感谢您
解决方案
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)
我执行了你的代码。它有很多错误。嘿,如果它符合目的,你能检查一下这个代码吗?
推荐阅读
- algorithm - 找出棋盘中所有可能的方格,不包括选定的单元格
- php - attach() 方法不附加外键值
- python-3.x - 用线连接蒙版点
- javascript - 循环遍历数组以将相同的值应用于其他复选框
- javascript - 这两个返回函数(使用冒号或普通格式)有什么区别?谢谢
- vagrant - centos vagrant box 在 vagrant up 命令上的每个实例都调用初始化脚本
- php - Symfony PHPUnit WebTestCase 与 RenderController
- postgresql - 在 UPDATE 子查询中未强制执行 LIMIT 子句
- windows - 有没有办法自动关闭“应用程序已停止响应”消息?
- javascript - Datepicker:如何验证 YEAR?