删除列表中的出现在字典元素 [英] Removing elements in list that appear in dict

查看:881
本文介绍了删除列表中的出现在字典元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我试着这样做:

 公开名单< D​​irectoryInfo的> RemoveDuplicates()
{
的foreach(KeyValuePair<字符串列表< D​​irectoryInfo的>>在B项)
{
VAR WTF = a.RemoveAll(X =>条目。 Value.Contains(X)及与放大器; x.Parent.Name == entry.Key);
}

返回;
}



这应该是告诉删除我原来DirectoryInfos列表重复功能是在我的字典B.我想通过列表的名称和父名,在我的字典父(键)和子文件夹(值)的列表进行比较,做到这一点。我的列表的居然是如此它会记住它的名字,知道其父DirectoryInfos但其所有子文件夹列表。对于dictionnary我想在索要循环以消除出现在删除它每一位家长的名字。我想这样做的,检查的跆拳道的价值和它说0。



修改



列表中包含项目=> 9.4.3.4(这是X或x.name),ACE(这是x.parent名称的父名称的一个例子)



Dictionnary b =>包含这正是我若去调试

  [0] {[NAME =IG-Version =6_6,System.Collections.Generic.List`1 [System.IO.DirectoryInfo]} System.Collections.Generic.KeyValuePair<字符串,System.Collections.Generic.List< System.IO.DirectoryInfo>> 



所以[0]位置的钥匙包含=


$内b $ b

 键name = \IG\-Version = \6_6\,串

和中值,我们有子生病显示U盘名称的样子列表。
+



  [0] {} 6.6.1.107_8 System.IO.DirectoryInfo 

所以我尝试看看是否有与父母的名字匹配器列表中检查的一个关键,如果我们有任何相同的文件夹内。删除



*********编辑2 *******



因此,这将是东西我想,出现在字典中b文件清单一个删除一个例子。所以最初我们分析这种格式的XML文件:

 <资产名称=SGL版本=9.6> 
< TestCase的名称=SST版本=9.6.1.111颠覆=/>
< /资产>



其中我们再像这样一本字典存储:

 键name = \SGL\-Version = \9_6\,串


[0 ] {} 9.6.1.111 System.IO.DirectoryInfo

在这里我们试着在列表中,如果进行比较它包含了同样的事情列表如下:



9.6.1.111(它包含文件夹的名称),但它也知道它的父(SLG_9_6)


解决方案

您不应该通过字典需要循环,这将是低效这样做。所有你需要的应该是这样的:

  VAR WTF = a.RemoveAll(X => b.ContainsKey(x.Parent .Name点)及和b [x.Parent.Name]。载有(X)); 



所以,你如果检查b 包含 Parent.Name ,如果确实如此,那么你检查,看是否列表中的该条目仅的包含 X



说了这么多,我不知道你原来的代码不应该反正工作 - 让你的实际问题可能是别的地方

您的问题实际上可能来自包含部分。由于的DirectoryInfo 是一个类,它会做的引用相等包含。换句话说,如果你在 A B 同一个对象的它只会工作$ C>。如果他们是不一样的,那么你将有比较值



是这样的:

  VAR WTF = a.RemoveAll(X => b.ContainsKey(x.Parent.Name)及和放大器; 
B [x.Parent.Name]。任何(Y => ; y.FullName == x.FullName));



假设比较全名的两个的DirectoryInfo 对象正是你想要的。


Hello everyone I tried doing this:

public List<DirectoryInfo> RemoveDuplicates()
    {
      foreach (KeyValuePair<string, List<DirectoryInfo>> entry in b)
      {
        var wtf = a.RemoveAll(x => entry.Value.Contains(x) && x.Parent.Name == entry.Key);
      }

      return a;
    }

It's supposed to be a function that tells remove duplicates in my original list of DirectoryInfos that are in my dictionary B. I want to do this by comparing the name and parent name of list A with the Parent (keys) and list of subfolders (values) in my dict. My list A actually is a list of DirectoryInfos but its all the subfolders so it remembers its name and knows its parent. For the dictionnary I'm trying to ask in the for loop to remove every parent name that appear in a remove it. I tried doing this and checking the value of "wtf" and it says 0.

EDIT

List a contains items => 9.4.3.4 (this is x or x.name), ACE (this is x.parent name an example of a parent name)

Dictionnary B => contains this exactly if I go in debug

[0] {[Name="IG"-Version="6_6", System.Collections.Generic.List`1[System.IO.DirectoryInfo]]} System.Collections.Generic.KeyValuePair<string,System.Collections.Generic.List<System.IO.DirectoryInfo>>

so inside of the [0] POSITION the key contains =

Key "Name=\"IG\"-Version=\"6_6\""   string

and in the value we have a list of subfolders ill show u how one name looks like. +

[0] {6.6.1.107_8}   System.IO.DirectoryInfo

So im trying to see if there is a key that matchers with a parent name in list a check if we have any of the same folders inside. remove them

*********EDIT 2*******

So this is going to be an example of something I want to remove in list a that appears in dictionary b. So originally we parsed a xml file of this format:

<Asset Name="SGL" Version="9.6">
      <TestCase Name="sst" Version="9.6.1.111" SubVersion=""/>
    </Asset>

Which we then store in a dictionary like so:

Key "Name=\"SGL\"-Version=\"9_6\""  string


[0] {9.6.1.111} System.IO.DirectoryInfo

where we tryin to compare in a list a if it contains the same thing list a looks like this:

9.6.1.111 (it contains the name of folder) but it knows its parent also (SLG_9_6)

解决方案

You shouldn't need to loop through the dictionary and it would be inefficient to do so. All you should need is something like:

var wtf = a.RemoveAll(x => b.ContainsKey(x.Parent.Name) && b[x.Parent.Name].Contains(x));

So you check if b contains the Parent.Name and if it does, then you check to see if the list for that entry only contains x.

Having said that, I'm not sure your original code shouldn't have worked anyway - so your actual problem might be somewhere else.

Your problem might actually come from the Contains part. Since DirectoryInfo is a class, it will do reference equality for Contains. In other words, it will only work if you have references to the same object in a and b. If they are not the same, then you will have to compare the values.

Something like:

var wtf = a.RemoveAll(x => b.ContainsKey(x.Parent.Name) &&
          b[x.Parent.Name].Any(y => y.FullName == x.FullName));

Assuming that comparing the FullName of two DirectoryInfo objects is what you wanted

这篇关于删除列表中的出现在字典元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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