从列表中全部删除,其中每行不包含其他列表中的任何项目
本文关键字:列表 其他 包含 项目 任何 删除 全部 | 更新日期: 2023-09-27 18:35:21
对于它的价值,我花了一段时间查看下面的帖子,该帖子是相关的,除了它正在处理具有多个属性而不是两个独立列表的同一列表,也不涉及文本包含比较而不是项目匹配。
如何从List
我有一个字符串列表,里面装满了名为"水果"的水果
Apple
Orange
Banana
我还有一个叫做产品的字符串列表,里面装满了一些水果(加上其他杂项信息)和其他一些产品。
ShoeFromNike
ApplePie
OrangeDrink
我需要从第二个列表中删除所有项目,其中每行字符串不包含水果列表中列出的任何项目。
最终结果将是产品列表仅包含:
ApplePie
OrangeDrink
我最好的迭代方法:
//this fails becaucse as I remove items the indexes change and I remove the wrong items (I do realize I could reverse this logic and if it meets the criteria ADD it to a new list and i'm sure there's a better way.)
for (int i = 0; i < products.Count(); i++)
{
bool foundMatch = false;
foreach (string fruit in fruits)
if (products[i].Contains(fruit))
foundMatch = true;
if (foundMatch == false)
products.Remove(products[i]);
}
我最好的 lambda 方法:
products.RemoveAll(p => !p.Contains(fruits.Select(f=> f)));
我个人喜欢使用 .Any(),它似乎更适合我;
products.RemoveAll(p => !fruits.Any(f => f.IndexOf(p, StringComparison.CurrentCultureIgnoreCase) >= 0));
这是我想出的,可能有更好的方法。
products.RemoveAll(p => fruits.Where(f=>p.Contains(f)).Count() == 0);
在英文中,它读作,删除该产品所含水果名称数量为零的所有产品。
(老实说,循环可能也不是一个糟糕的选择,因为它将来可能会更具可读性)。
如果你想
保持循环,你也可以做你正在做的事情,但颠倒循环的顺序......
for (int i = products.Count()- 1; i >= 0; i--)
{
bool foundMatch = false;
foreach (string fruit in fruits)
if (products[i].Contains(fruit))
foundMatch = true;
if (foundMatch == false)
products.Remove(products[i]);
}
这样可以避免在索引循环之前从列表中删除。