擅长:python、mysql、java
<p>我认为您需要的是<code>set</code>内置类。遵循<a href="https://docs.python.org/3/tutorial/datastructures.html#sets" rel="nofollow noreferrer">tutorial within the official documentation</a>是一个很好的起点:</p>
<p>可以通过文字语法或函数调用创建集合:</p>
<pre><code>>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> basket = set(['apple', 'orange', 'apple', 'pear', 'orange', 'banana'])
</code></pre>
<p>它们不包含重复项:</p>
<pre><code>>>> print(basket)
{'orange', 'banana', 'pear', 'apple'}
</code></pre>
<p>他们支持非常快速的成员资格测试,这就是为什么我认为他们适合你手头的问题:</p>
<pre><code>>>> 'orange' in basket
True
</code></pre>
<p>集合可以按需要增长:</p>
<pre><code>>>> basket.add('grape')
</code></pre>