检查列表是否包含其他字符串 [英] Check if list doens't contain other strings

查看:121
本文介绍了检查列表是否包含其他字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。

我相信,这个问题已经得到了解答,但我找不到具有低表达能力的问题。所以,假设我有一个名为list1的列表:

Hello.
I am sure, that this question has been already answered, but I just can't find the question with my low expression abilities. So, let's say I have a list named list1:

a, b, c



我想要chech如果另一个列表(list2)不包含list1中的其他元素。

所以如果list2是:


I want to chech if another list (list2) doesn't contain other elements than these in list1.
So if list2 is:

a, b, c, d



它将返回false,但如果缺少list1的某些元素,它仍将返回true。希望你理解我,抱歉我的英语不好,还在学习。感谢您的阅读!


It will return false, but also if some elements of list1 are missing, it will still return true. Hopefully you understood me and sorry for my bad english, still learning. Thanks for reading!

推荐答案

使用下面的代码我得到了以下结果:



Using the code below I got the following results:

100000000 00:00:45.9990976
200000000 00:00:21.1352109
300000000 00:00:17.3039397





所以,对于给定的数据 ...

Set.IsSupersetOf 是最快的,我推荐它满足您的需求(无论它们是什么)。

Set.IsSubsetOf 不是那么快 - 我希望它必须迭代更多的List 。

Linqish List.Except 是最糟糕的,包括如果你只想要一个布尔值那么就没有必要产生一个集合的情况然后计算集合中的项目。



考虑一下,在寻找布尔值时,只要在目标外找到一个项目,就可以将过程短路。我当然希望 IsSupersetOf IsSubsetOf 这样做。 除了当然短路然后它会让你执行另一个操作(比如 Count )才能得到你的结果。





So, for the given data...
Set.IsSupersetOf is quickest and I recommend it for your needs (whatever they are).
Set.IsSubsetOf is not quite as quick -- I expect it has to iterate more of the List.
And the Linqish List.Except is worst, including the situation that if you just want a boolean then there is no need to produce a collection and then count the items in the collection.

Consider, that when seeking a boolean, the process can be short-circuited as soon as one item is found outside the target. I certainly hope that IsSupersetOf and IsSubsetOf do that. Except certainly does not short-circuit and then it makes you perform another operation (like Count) before you can get your result.

using System.Linq ;
using StringList=System.Collections.Generic.List<string> ;
using StringSet=System.Collections.Generic.HashSet<string> ;

        int reps = 100000000 ;

        StringList list1 = new StringList { "a" , "b" , "c" , "z" } ;
        StringList list2 = new StringList { "a" , "b" , "c" , "d" } ;
        StringSet  set1  = new StringSet  ( list1 ) ;
        StringSet  set2  = new StringSet  ( list2 ) ;

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch() ;


        sw.Restart() ;

        for ( int i = 0 ; i < reps ; i++ )
          result += list2.Except(list1).Count()==0 ? 0 : 1 ;

        sw.Stop() ;

        System.Console.WriteLine ( "{0} {1}" , result , sw.Elapsed ) ;


        sw.Restart() ;

        for ( int i = 0 ; i < reps ; i++ )
          result += set2.IsSubsetOf ( list1 ) ? 0 : 1 ;

        sw.Stop() ;

        System.Console.WriteLine ( "{0} {1}" , result , sw.Elapsed ) ;


        sw.Restart() ;

        for ( int i = 0 ; i < reps ; i++ )
          result += set1.IsSupersetOf ( list2 ) ? 0 : 1 ;

        sw.Stop() ;

        System.Console.WriteLine ( "{0} {1}" , result , sw.Elapsed ) ;


基于tis文章:如何:找到两个列表之间的集合差异(LINQ) [ ^ ],您可以使用Linq查找两个字符串列表之间的差异:



Based on tis article: How to: Find the Set Difference Between Two Lists (LINQ)[^], you can use Linq to find differences between two string lists:

List<string> list1 = new List<string>{"a","b","c"};
List<string> list2 = new List<string>{"a","b","c", "d"};

var hasMatch = list2.Except(list1);





退货: d





感谢您提出宝贵意见。它帮助我改进了我的答案。



比较两个字符串列表的另一个interestinf方法是: SequenceEqual ,其中返回布尔值。



Returns: d


Thank you for all valuable comments. It helps me to improve my answer.

Another interestinf method to compare two string list is: SequenceEqual, which returns boolean value.

bool listsAreEqual = list2.SequenceEqual(list1);





如需了解更多信息,请参阅: Enumerable.SequenceEqual< TSource>方法(IEnumerable< TSource>,IEnumerable< TSource>) [ ^ ]。


这篇关于检查列表是否包含其他字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆