小编vic*_*oom的帖子

在 O(1) 时间 Python 中查找字典中的最小值

如果可能的话,我需要一种方法来在 O(1) 时间或真正的任何次线性时间内在充满 Node 对象的字典中找到最小值。

这是我需要的示例:

'''
 Nodes have 4 attributes: 
  - stack
  - backwards
  - forwards
  - total_score
'''
dict = {str(Node1.stack): Node1, 
        str(Node2.stack): Node2, ... } # note that keys are the stacks as strings

key, smallest = min(frontier.items(), key=lambda pair: pair[1].total_score)
( ^^^ something better than this! ^^^ )
Run Code Online (Sandbox Code Playgroud)

上面的最后一行(关键,最小...)是我到目前为止所拥有的。它工作正常,但速度太慢。我在网上读到 min() 函数需要 O(n) 时间。我有很多节点要处理,所以速度更快会很棒。

编辑之前应该提到过,但这是在 A* 算法中运行的 - 前沿动态更新。我需要能够做的操作是:

  1. 在 O(1) 中找到最小值,或至少 < O(n)
  2. 快速更新特定元素的值
  3. 轻松访问属性

python big-o dictionary time-complexity python-3.x

2
推荐指数
1
解决办法
1246
查看次数

导入“气流”无法解决

VSCode 无法导入气流模块。

VSCode 错误

Python == 3.10.6, apache-airflow == 2.5.3

Airflow 服务器工作正常,可以创建 DAG,但 pylint 和 pylance 表示无法导入 Airflow。Airflow 是在没有 docker 的情况下配置的。

python python-3.x airflow airflow-2.x

2
推荐指数
1
解决办法
3488
查看次数