擅长:python、mysql、java
<p>您可以使用<code>list</code>个<code>sets</code>规则,并要求带有内部列表的任何规则<a href="https://docs.python.org/3.7/library/stdtypes.html#frozenset.intersection" rel="nofollow noreferrer">intersection</a>与<code>set</code>相同(即,集合中的所有</em>项也存在于内部列表中):</p>
<pre><code>records = [['eggs', 'milk', 'butter'], ['ham', 'spam', 'milk'], ['cereal', 'skittles']]
list_of_rules = [{'milk', 'eggs'}, {'milk','ham'}]
# this list comprehension only filters for exact matches
# take the full inner list if all things in any rule are in this inner list
results = [ x for x in records if any( p.intersection(x) == p for p in list_of_rules)
print(results)
</code></pre>
<p>输出:</p>
<pre><code>[['eggs', 'milk', 'butter'], ['ham', 'spam', 'milk']]
</code></pre>