Dijkstra 算法的一种标准实现使用堆来存储从起始节点 S 到所有未探索节点的距离。使用堆的论据是我们可以有效地弹出与它的最小距离,in O(log n)
。然而,为了保持算法的不变性,还需要更新堆中的一些距离。这包括:
我知道O(log n)
如果知道该元素在堆中的位置,则可以从堆中弹出非最小元素。但是,我无法理解在 Dijkstra 算法的情况下如何知道这个位置。听起来二叉搜索树更合适。
更一般地说,我的理解是,堆比平衡二叉搜索树做得更好的唯一一件事就是访问(而不删除)最小元素。我的理解正确吗?
algorithm dijkstra priority-queue binary-search-tree data-structures
所以我正在写一个双向链表的实现。这是实现单个节点的类的构造函数:
DNode::DNode(const int a_key, const DNode* a_prev, const DNode* a_next)
: key(a_key), prev(a_prev), next(a_next) {}
Run Code Online (Sandbox Code Playgroud)
我写这本书的原因const int a_key, const DNode* a_prev, const DNode* a_next
是因为构造函数没有理由修改它们。因此,我只想保护自己不要在构造函数内进行任何不必要的修改。这是一件好事吗?
编译输出以下错误:
dnode.cpp:6:89:错误:无法使用类型为const DNode *'的左值初始化类型为'DNode *'的成员子对象DNode :: DNode(const int a_key,const DNode * a_prev,const DNode * a_next) :键(a_key),上一个(a_prev),下一个(a_next){}
dnode.cpp:6:103:错误:无法使用类型为const DNode *'的左值初始化类型为'DNode *'的成员子对象DNode :: DNode(const int a_key,const DNode * a_prev,const DNode * a_next) :键(a_key),上一个(a_prev),下一个(a_next){}
我不明白该错误信息。DNode*
是指针类型,而不是左值。欢迎任何帮助。
===编辑===
我将代码修改为以下内容。
dnode.h
class DNode {
public:
//
DNode( const int a_key, const DNode& a_prev, const DNode& a_next );
// …
Run Code Online (Sandbox Code Playgroud) 所以我想测试列表是否已排序.阅读本页后,我这样做了:
ll = [ 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 ]
all(b >= a for a, b in zip(ll, ll[1:]) )
Run Code Online (Sandbox Code Playgroud)
产量
<generator object <genexpr> at 0x10d9ecaa0>
Run Code Online (Sandbox Code Playgroud)
好的,所以all()
返回发电机.但这是Python文档所说的all()
:
如果iterable的所有元素都为true(或者iterable为空),则返回True
我错过了什么?
关于gunicorn
:我期望最佳工人数量为$num_cores
或$num_cores-1
,即每个工人都有自己的核心。但是 gunicorn文档给出了以下指南:
Gunicorn 依靠操作系统在处理请求时提供所有负载平衡。通常我们建议 (2 x $num_cores) + 1 作为开始的工作人员数量。虽然不太科学,但该公式基于这样一个假设,即对于给定的内核,一个 worker 将从套接字读取或写入,而另一个 worker 正在处理请求。
我不明白解释。这是否表明同一个内核可以同时用于 1)从套接字读取或写入以及 2)处理请求?(单核可以做这样的事情吗?)
我们在Python中称之为"堆栈"是什么?它是CPython的C堆栈吗?我读到Python堆栈帧是在堆中分配的.但我认为堆栈的目标是...堆栈堆栈帧.那堆栈到底是做什么的?
不久前我在OSX上安装了python科学环境(numpy,scipy,matplotlib,pandas,ipython),使用brew和pip或easy_install(或其中2个,我不记得了).现在,如果尝试运行ipython,我得到:
-> ipython
-bash: ipython: command not found
Run Code Online (Sandbox Code Playgroud)
然后我做了:
-> sudo find . -iname "*ipython*"
Password:
find: ./dev/fd/3: Not a directory
find: ./dev/fd/4: Not a directory
./Users/jfk/.ipython
./usr/local/Cellar/matplotlib/1.2.0/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_console_highlighting.py
./usr/local/Cellar/matplotlib/1.2.0/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_console_highlighting.pyc
./usr/local/Cellar/matplotlib/1.2.0/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_directive.py
./usr/local/Cellar/matplotlib/1.2.0/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_directive.pyc
./usr/local/Cellar/matplotlib/1.2.1/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_console_highlighting.py
./usr/local/Cellar/matplotlib/1.2.1/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_console_highlighting.pyc
./usr/local/Cellar/matplotlib/1.2.1/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_directive.py
./usr/local/Cellar/matplotlib/1.2.1/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_directive.pyc
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/doc/ipython
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/doc/ipython/examples/core/ipython-get-history.py
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/doc/ipython/examples/core/ipython-qtconsole.desktop
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/doc/ipython/examples/core/ipython-sh.desktop
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/doc/ipython/examples/core/ipython.desktop
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/doc/ipython/examples/core/ipython_here_shell_extension.reg
./usr/local/Cellar/python/2.7.4/Frameworks/Python.framework/Versions/2.7/share/man/man1/ipython.1
./usr/local/lib/python2.7/site-packages/IPython
./usr/local/lib/python2.7/site-packages/IPython/config/profile/cluster/ipython_config.py
./usr/local/lib/python2.7/site-packages/IPython/config/profile/cluster/ipython_config.pyc
./usr/local/lib/python2.7/site-packages/IPython/config/profile/math/ipython_config.py
./usr/local/lib/python2.7/site-packages/IPython/config/profile/math/ipython_config.pyc
./usr/local/lib/python2.7/site-packages/IPython/config/profile/pysh/ipython_config.py
./usr/local/lib/python2.7/site-packages/IPython/config/profile/pysh/ipython_config.pyc
./usr/local/lib/python2.7/site-packages/IPython/config/profile/sympy/ipython_config.py
./usr/local/lib/python2.7/site-packages/IPython/config/profile/sympy/ipython_config.pyc
./usr/local/lib/python2.7/site-packages/IPython/frontend/html/notebook/static/codemirror/README-IPython.rst
./usr/local/lib/python2.7/site-packages/IPython/frontend/html/notebook/static/codemirror/theme/ipython.css
./usr/local/lib/python2.7/site-packages/IPython/frontend/qt/console/ipython_widget.py
./usr/local/lib/python2.7/site-packages/IPython/frontend/qt/console/ipython_widget.pyc
./usr/local/lib/python2.7/site-packages/IPython/frontend/qt/console/resources/icon/IPythonConsole.svg
./usr/local/lib/python2.7/site-packages/IPython/frontend/qt/console/rich_ipython_widget.py
./usr/local/lib/python2.7/site-packages/IPython/frontend/qt/console/rich_ipython_widget.pyc
./usr/local/lib/python2.7/site-packages/ipython-0.13.2-py2.7.egg-info
./usr/local/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_console_highlighting.py
./usr/local/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_console_highlighting.pyc
./usr/local/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_directive.py
./usr/local/lib/python2.7/site-packages/matplotlib/sphinxext/ipython_directive.pyc
./usr/local/share/python/ipython
Run Code Online (Sandbox Code Playgroud)
我很惊讶没有看到任何/ bin目录.
然后我尝试重新安装ipython
-> pip install ipython
Requirement already satisfied (use --upgrade to upgrade): ipython in …
Run Code Online (Sandbox Code Playgroud) 我知道如何使用发电机,但我对它们的内部结构一无所知.我试过这个:
In [4]: def f(): yield 1
In [6]: type(f())
Out[6]: generator
Run Code Online (Sandbox Code Playgroud)
现在我拆开它:
In [7]: dis.dis(f)
1 0 LOAD_CONST 1 (1)
3 YIELD_VALUE
4 POP_TOP
5 LOAD_CONST 0 (None)
8 RETURN_VALUE
Run Code Online (Sandbox Code Playgroud)
为什么操作码会return None
在f
实际返回生成器时提示?
我读到一切都是Python中的一个对象.那么,什么是2:4
在L[2:4]
?它是切片的对象吗?当我输入2:4
解释器时,SyntaxError
会引发一个.
所以下面的内置类有一个__eq__
属性,(我假设)是你可以测试它们的实例是否相等的原因:
>>> 1.2.__eq__( 1.2 )
True
>>> 1.2 == 1.2
True
>>> 'hello'.__eq__( 'hi' )
False
>>> 'hello' == 'hi'
False
>>> [1,2].__eq__( [1,2] )
True
>>> [1,2] == [1,2]
True
Run Code Online (Sandbox Code Playgroud)
然后我很惊讶地看到int
物体没有__eq__
,但我们仍然可以比较它们:
>>> hasattr( 1, '__eq__' )
False
>>> 1 == 2
False
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?我是否误解__eq__
了平等运算符之间的关系?
假设我创建了2个列表:
l = range(pow(10,6))
m = range(pow(10,6))
Run Code Online (Sandbox Code Playgroud)
我们来计算共享对象的数量:
import itertools
sum(mi is li for mi, li in itertools.izip(m,l))
257
Run Code Online (Sandbox Code Playgroud)
百万中只有257个?MMH.我们打印出来:
print [ mi for mi, li in itertools.izip(m,l) if mi is li ]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, …
Run Code Online (Sandbox Code Playgroud) 我正在学习偏见以及何时使用它们.在这个关于partials vs lambdas的页面中,接受的答案解释了partials
over 的优点之一lambdas
是partials具有对内省有用的属性.因此我们可以使用partials来执行以下操作:
import functools
f = functools.partial(int, base=2)
print f.args, f.func, f.keywords
((), int, {'base': 2})
Run Code Online (Sandbox Code Playgroud)
实际上,我们不能这样做lambdas
:
h = lambda x : int(x,base=2)
print h.args, h.func, h.keywords
AttributeError: 'function' object has no attribute 'args'
Run Code Online (Sandbox Code Playgroud)
但实际上,我们不能用"普通"Python函数做到这一点:
def g(x) :
return int(x,base=2)
print g.args, g.func, g.keywords
AttributeError: 'function' object has no attribute 'args'
Run Code Online (Sandbox Code Playgroud)
为什么partials比普通的Python函数有更多的功能?这种设计的目的是什么?内省被认为对正常功能无用吗?