擅长:python、mysql、java
<p>据我所知,没有任何内在的东西能像你想要的那样表现。严格地说,这不是一个真正的子集,因为它应该像在Python中那样进行集合比较(在Python中,集合中的每个项目都是唯一的),但是创建一个集合应该很简单。在</p>
<pre><code>public static bool IsSubsetOf<TSource>(this IEnumerable<TSource> lhs, IEnumerable<TSource> rhs)
{
// O(m+n)
var contents = rhs.ToList();
foreach (var item in lhs)
{
if (!contents.Remove(item))
return false;
}
return true;
}
</code></pre>
^{pr2}$
<p>如果你想要真正的集合机制,它同样简单。在</p>
<pre><code>public static bool IsTrueSubsetOf<TSource>(this IEnumerable<TSource> lhs, IEnumerable<TSource> rhs)
{
return new HashSet<TSource>(lhs).IsSubsetOf(rhs);
}
</code></pre>