删除字典中的重复项

时间:2019-04-10 18:37:18

标签: vb.net

嗨,我有一个字典,里面装有由正则表达式匹配的实体。它会正确提取所有数据,除了还会带来重复数据。如何防止重复数据进入?

这是我的代码

    Dim largeFilePath As String = newMasterFilePath
    Dim lines1 = File.ReadLines(largeFilePath).ToList 'don't use ReadAllLines
    Dim reg = New Regex("\<\!NOTATION.*$|\<\!ENTITY.*$", RegexOptions.IgnoreCase)
    Dim entities = From line In lines1
                   Where reg.IsMatch(line)

    Dim dictionary As New Dictionary(Of Integer, String)
    Dim idx = -1
    For Each s In entities
        idx = lines1.IndexOf(s, idx + 1)
        dictionary.Add(idx, s)
    Next

    Dim deletedItems = 0
    For Each itm In dictionary
        lines1.RemoveAt(itm.Key - deletedItems)
        deletedItems += 1
    Next

    For Each s In dictionary.Values
        lines1.Insert(1, s)
    Next

我希望每个项目只有一个条目。

这是示例代码

<!DOCTYPE DOC PUBLIC "-//USA-DOD//DTD 38784STD-BV7//EN"[
<!ENTITY cdcs_5-35.wmf SYSTEM "graphics\CDCS_5-35.wmf" NDATA wmf>
<!ENTITY cdcs_2-2a.wmf SYSTEM "graphics\CDCS_2-2A.wmf" NDATA wmf>
<!NOTATION bmp SYSTEM "bmp">
<!NOTATION svg SYSTEM "svg">
<!NOTATION png SYSTEM "png">
<doc service="xs" docid="BKw46" docstat="formal" verstatpg="ver" cycle="1" chglevel="1">
<front numcols="1">
<idinfo>
<?Pub Lcl _divid="100" _parentid="0">
<tmidno>Life with Pets</tmidno>
<chgnum>Change 1</chgnum>
<chgdate>2 August 2018</chgdate>
<chghistory>
<!NOTATION bmp SYSTEM "bmp">
<!NOTATION svg SYSTEM "svg">
<!NOTATION png SYSTEM "png">
<chginfo>
<chgtxt>Change 1</chgtxt>
<date>2 August 2018</date>
</front>
<!ENTITY cdcs_2-19.wmf SYSTEM "graphics\CDCS_2-19.wmf" NDATA wmf>
<!ENTITY cdcs_3-5.wmf SYSTEM "graphics\CDCS_3-5.wmf" NDATA wmf>
<!ENTITY cdcs_4-48.wmf SYSTEM "graphics\CDCS_4-48.wmf" NDATA wmf>
<body numcols="1">
<chapter>
<title>This is chapter 1</title>
<!ENTITY cdcs_2-5.wmf SYSTEM "graphics\CDCS_2-5.wmf" NDATA wmf>
<!ENTITY cdcs_2-24.wmf SYSTEM "graphics\CDCS_2-24.wmf" NDATA wmf>
<para0>
<title>Climb the ladder immedietly</title>
<para>Retrieve the cat.</para></para0></chapter>
<chapter>
<title>Don't forget to feed the dog</title>
<!ENTITY cdcs_2-5.wmf SYSTEM "graphics\CDCS_2-5.wmf" NDATA wmf>
<!ENTITY cdcs_2-24.wmf SYSTEM "graphics\CDCS_2-24.wmf" NDATA wmf>
<para0>
<!ENTITY cdcs_4-48.wmf SYSTEM "graphics\CDCS_4-48.wmf" NDATA wmf>
<title>Prep for puppies</title>
<para>Puppies are cute</para></para0>
</chapter>
</body>
</doc>

谢谢您在此方面的帮助。 最高

2 个答案:

答案 0 :(得分:1)

或者,在添加之前检查是否重复:

For Each s In entities
    If Not dictionary.TryGetValue(lines1.IndexOf(s, idx + 1), s) Then
        idx = lines1.IndexOf(s, idx + 1)
        dictionary.Add(idx, s)
    End If
Next

答案 1 :(得分:0)

将此行Person instanced Person instanced Person instanced StreetAddress: 123 London Road, Postcode: SW12BC, City: London, CompanyName: Fab rikam, Position: Engineer, AnnualIncome: 123000 更改为dictionary.Add(idx, s)

然后:

dictionary.Add(idx, s.Trim)

结果,所有重复项均已删除:

Dim uniqueDict = dictionary.GroupBy(Function(itm) itm.Value).
                   Select(Function(group) group.First()).
                   ToDictionary(Function(itm) itm.Key, Function(itm) itm.Value)

For Each s In uniqueDict.Values
     lines.Insert(1, s)
Next