python - 在特定键值处开始字典 for 循环

标签 python performance python-2.7 for-loop dictionary

代码如下:

编辑**** 请不要再说“无序的字典回复是不可能的”。我几乎已经知道了。我发表这篇文章是因为它可能是可能的或者有人有可行的想法。

#position equals some set of two dimensional coords
for name in self.regions["regions"]:  # I want to start the iteration with 'last_region'
    # I don't want to run these next two lines over every dictionary key each time since the likelihood is that the new
    # position is still within the last region that was matched.
    rect = (self.regions["regions"][name]["pos1"], self.regions["regions"][name]["pos2"])
    if all(self.point_inside(rect, position)):
        # record the name of this region in variable- 'last_region' so I can start with it on the next search...
        # other code I want to run when I get a match
        return
return # if code gets here, the points were not inside any of the named regions

希望代码中的注释能够很好地解释我的情况。假设我最后在区域“delta”内(即,键名是 delta,值将是定义其边界的坐标集)并且我还有 500 个区域。我第一次发现自己处于三角洲区域时,代码可能直到(假设)第 389 次迭代才发现这一点……所以它生成了 388 all(self.point_inside(rect, position)) 在它发现之前的计算。由于下次运行时我可能仍处于增量状态(但每次代码运行时我都必须验证),如果键“delta”是第一个被 for 循环检查的键,那将会很有帮助。

对于许多不同的用户,这个特定的代码可以每秒运行多次。所以速度是至关重要的。设计是这样的,用户通常不在一个区域中,可能需要循环所有 500 条记录,并且会在没有匹配的情况下退出循环,但我想通过加快它来加快整个程序的速度那些目前在其中一个地区的人。

我不想要以任何特定顺序对字典进行排序等的额外开销。我只希望它开始查找它成功匹配的最后一个 all(self.point_inside(rect, position ))

也许这会有所帮助。以下是我正在使用的字典(仅显示第一条记录),因此您可以看到我在上面编码的结构......是的,尽管名称为“rect”代码,它实际上检查立方体区域中的点。

{"regions": {"shop": {"flgs": {"breakprot": true, "placeprot": true}, "dim": 0, "placeplayers": {"4f953255-6775-4dc6- a612-fb4230588eff": "SurestTexas00"}, "breakplayers": {"4f953255-6775-4dc6-a612-fb4230588eff": "SurestTexas00"}, "protected": true, "banplayers": {}, "pos1": [ 5120025, 60, 5120208], "pos2": [5120062, 73, 5120257], "ownerUuid": "4f953255-6775-4dc6-a612-fb4230588eff", "accessplayers": {"4f953255-6775-4dc6-a615-8fb84 ": "SurestTexas00"}},更多,更多,更多...

最佳答案

您可以尝试在 dict 的自定义子类中实现一些缓存机制。

你可以在__init__中设置一个self._cache = None,添加一个类似set_cache(self, key)的方法来设置缓存最后覆盖 __iter__yield self._cache 在调用默认的 __iter__ 之前。

但是,如果您考虑 this stackoverflow answer,那可能有点麻烦还有this one .

对于您问题中所写的内容,我会尝试在您的代码中实现此缓存逻辑。

def _match_region(self, name, position):
    rect = (self.regions["regions"][name]["pos1"], self.regions["regions"][name]["pos2"])
    return all(self.point_inside(rect, position))


if self.last_region and self._match_region(self.last_region, position):
    self.code_to_run_when_match(position)
    return

for name in self.regions["regions"]:
    if self._match_region(name, position):
        self.last_region = name
        self.code_to_run_when_match(position)
        return
return # if code gets here, the points were not inside any of the named regions

关于python - 在特定键值处开始字典 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31106100/

相关文章:

python - 在 python 中每 5 分钟运行一次与系统时钟同步的函数的最佳方法是什么?

Python 正则表达式可选捕获组,具有正向前瞻功能

python - TensorFlow while_loop 将变量转换为常量?

Java 性能 - 如何以高性能将大数组写入磁盘/SD 卡?

Exadata 上的 Oracle 数据库。更新运行数小时,并行提示不起作用

c++ - 在哪里为最高效的代码定义一次性常量?

将plt文件转换为特定格式的gcode的Python代码

python - 明确关闭文件重要吗?

python - 使用 python 对药片进行雕刻印记检测

python - Python 中的可重入子进程?