简体   繁体   English

.NET是否与Python的issubset方法等效?

[英]Does .NET have an equivalent for Python's issubset method?

UPDATE: 更新:

As @Blender pointed out in Python set('aab').issubset(set('abb')) == True . 正如@Blender在Python中指出的那样set('aab').issubset(set('abb')) == True For my situation this needs to return false. 对于我的情况,这需要返回false。 The number of each character needs to be taken into account. 需要考虑每个字符的数量。


Basically I have two strings and I would like to determine if one is a subset of another. 基本上,我有两个字符串,我想确定一个字符串是否为另一个字符串的子集。 Example: 例:

String A: abcd
String B: dbace
String A is a subset of string B

Characters can be in any order and there can be repeating numbers of characters. 字符可以是任意顺序,并且可以重复出现多个字符。 I had tried ordering the strings and then using String.StartsWith, but this does not work in certain situations. 我尝试过排序字符串,然后使用String.StartsWith,但这在某些情况下不起作用。 Example: 例:

String A: abcdd
string B: abbcdd
Ordering these and using StartsWith returns false because string B has two "b"s

I did some looking around and found Python's issubset method which appears to do what I want, so I'm curious if anyone has come across its equivalent in .NET (or an effective method someone has come up with on their own). 我四处张望,发现Python的issubset方法似乎可以满足我的要求,所以我很好奇是否有人遇到过.NET中的等效方法(或者有人自己提出了一种有效的方法)。

NOTE: I am looking for subsets, not anagrams. 注意:我在寻找子集,而不是字谜。

There's nothing built-in that I know of that behaves as you want it. 据我所知,没有内置函数可以像您期望的那样运行。 Strictly speaking, this isn't a real subset as it should be doing set comparisons as it is in Python (where every item in a set is unique) but it should be simple to cook one up. 严格来说,这不是真正的子集,因为它应该像Python中那样进行集合比较(集合中的每个项目都是唯一的),但是烹饪起来应该很简单。

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;
}
"aab".IsSubsetOf("abb");      // false
"foo".IsSubsetOf("food");     // true
"foo".IsSubsetOf("goof");     // true
"bar".IsSubsetOf("barf");     // true
"abcd".IsSubsetOf("dbace");   // true
"abcdd".IsSubsetOf("abbcdd"); // true

If you want true set mechanics, it is just as simple. 如果您想要真正的设定机制,那就很简单。

public static bool IsTrueSubsetOf<TSource>(this IEnumerable<TSource> lhs, IEnumerable<TSource> rhs)
{
    return new HashSet<TSource>(lhs).IsSubsetOf(rhs);
}

I think the best solution is to sort both of them and check the subset by Contains method. 我认为最好的解决方案是对它们进行排序,并通过Contains方法检查子集。

new String(A.OrderBy(o=> o)).Contains(new String(B.OrderBy(o=>o)))

UPDATE: 更新:

new String(A.OrderBy(o=> o)
            .Distinct())
     .Contains(new String(B.OrderBy(o=>o)
                           .Distinct()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET是否具有与Java的.properties文件相同的功能? - Does .NET have an equivalent to Java's .properties files? .NET是否与Delphi的VCL组件模型等效? - Does .NET have an equivalent to Delphi's VCL component model? .NET是否具有与Java的ConcurrentHashMap等效的Dictionary实现? - Does .NET have a Dictionary implementation that is equivalent to Java's ConcurrentHashMap? Python 相当于 .Net 的密封类 - Python's equivalent of .Net's sealed class 对于 .NET IEnumerable,是否有等效于 Python 的 enumerate() - Is there an equivalent to Python's enumerate() for .NET IEnumerable .NET是否具有与XML等效的Json.NET类型功能? - Does .NET have an equivalent of Json.NET type functionality for XML? .NET 等效于 Python 的 imghdr 的框架 - .NET Framework equivalent for Python's imghdr Silverlight上的.NET在Objective-C中是否具有firstObjectCommonWithArray的等效项? - Does .NET on Silverlight have the equivalent of firstObjectCommonWithArray in Objective-C? Java是否具有等效于.NET资源(.resx)文件的本地化? - Does Java have an equivalent to .NET resource (.resx) files for localization? Java 是否具有与 C# 元组等效的变量类型? - Does Java have an equivalent variable type to C#'s Tuple?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM