擅长:python、mysql、java
<p><code>if find_num_3 not in {None, i, j}</code></p>
<p>使用set而不是list或tuple,因为检查集合中是否存在元素更有效。复杂性<code>~O(1)</code>而不是<code>O(n)</code>。你知道吗</p>
<p>集合将其数据存储为键/值对。密钥是存储对象的哈希。这就是为什么不能在一个集合中存储具有相同哈希的多个对象。由于哈希冲突,在一个集合中的存在性检查有时可能比O(1)多一点。你知道吗</p>
<p><a href="https://www.asmeurer.com/blog/posts/what-happens-when-you-mess-with-hashing-in-python/" rel="nofollow noreferrer">Here is a nice article to better understand hashes and sets.</a></p>
<p><strong>编辑</p>
<p>正如@chepner所指出的,当值仅在运行时已知时(如问题中所述),使用tuple比使用set更有效,因为set实例化比tuple实例化长。你知道吗</p>