<p>一般来说,答案是<strong>否。</strong>python源代码,@Christophe和@Marcin(un)很有帮助地指出,元素是按照它们在哈希表中的出现顺序弹出的。因此,pop-order(以及可能的迭代顺序)<em>是确定的,但仅适用于<em>固定的</em>散列值。
根据<a href="https://docs.python.org/3.5/reference/datamodel.html#object.__hash__">^{<cd1>}</a>文档中的<strong>注释</strong>,这是数字的情况,但字符串的<em>不是</em>,顺便说一下,这也直接涉及到您的问题:</p>
<blockquote>
<p>Note by default the <strong>hash</strong>() values of str, bytes and datetime objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python. </p>
<p>[ ... ]</p>
<p>Changing hash values affects the iteration order of dicts, sets and other mappings. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).</p>
</blockquote>
<p><strong>编辑:</strong>正如@Marcin指出的,我引用的链接不适用于Python 2。
默认情况下,哈希随机化<a href="https://docs.python.org/3/whatsnew/3.3.html#summary-release-highlights">became the default with Python 3.3.</a>Python 2.7没有有意的非确定性字符串哈希。</p>
<p>一般来说,对于散列不是其值的可重复函数的任何对象(例如,如果散列基于内存地址),这是一个问题。但是相反,如果为集合中的对象定义自己的<code>__hash__</code>方法,则可以预期它们将以可复制的顺序返回。(前提是设备的历史和平台保持固定)。</p>