比较两个列表并删除相同的最有效方法 [英] Most efficient way to compare two lists and delete the same

查看:29
本文介绍了比较两个列表并删除相同的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个列表并将有效单词放入一个新列表中.

I want to compare two lists and get the valid words into a new list.

var words = new List<string>();
var badWords = new List<string>();

//this is just an example list. actual list does contain 700 records
words.Add("Apple");
words.Add("Moron");
words.Add("Seafood");
words.Add("Cars");
words.Add("Chicken");
words.Add("Twat");
words.Add("Watch");
words.Add("Android");
words.Add("c-sharp");
words.Add("Fool");

badWords.Add("Idiot");
badWords.Add("Retarded");
badWords.Add("Twat");
badWords.Add("Fool");
badWords.Add("Moron");

我正在寻找最有效的方法来比较列表并将所有好"词放入新列表中.finalList 不应包含Moron"、Twat"和Fool".

I am looking for most efficient way to compare the lists and put all the 'good' words into a new list. The finalList shouldn't contain "Moron", "Twat" and "Fool".

var finalList = new List<string>();

还是不需要新建List?我很高兴听到你的想法!

Or is it unnecessary to create a new List? I am happy to hear your ideas!

提前致谢

推荐答案

Use EnumerableExcept 函数存储在 System.Linq 命名空间

Use EnumerableExcept function storing in System.Linq namespace

finalList = words.Except(badWords).ToList();

节省时间的最有效方法也是最快的方法,因为Except 实现使用Set,速度很快

Most efficient way to save your time and also the fastest way to do it, because Except implementation uses Set, which is fast

这篇关于比较两个列表并删除相同的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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