0

我必须列出许多变体。第一个列表样本

清单 1)

hkdhksa
OP-ID:111112
jklfjdlkfsd
hfldhfjksdf
OP-ID:111113
ghjg
OP-ID:111114
OP-ID:111115
gjgjhghgjhg
OP-ID:111116
OP-ID:111117
OP-ID:111118

清单 2)

操作 ID:111112
操作 ID:11113
操作 ID:111114
操作 ID:111115
操作 ID:111117

结果将是: OP-ID: 11118 不在列表 2 中

Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    'Declare two dictionaries. The key for each will be 
    ' the text from the input line up to,
    'but not including the first ",". 
    ' The valus for each will be the entire input line.

    'Dim file1 As New HashSet(Of String) '!
    Dim file1 As New Dictionary(Of String, String)
    Dim file2 As New Dictionary(Of String, String)

    For Each line As String In System.IO.File.ReadAllLines(TEST1)
        Dim part() As String = line.Split(",")

        If line = ("OP-ID: ") Like "OP-ID:*" Then
            If Not file1.ContainsKey(part(0)) Then file1.Add(part(0), line)
        End If

    Next

    For Each line As String In System.IO.File.ReadAllLines(TEST2)
        Dim part() As String = line.Split(",")
        If Not file2.ContainsKey(part(0)) Then file2.Add(part(0), line) '!
    Next


    Dim keysInList1ThatAreNotInList2 = file1.Keys.Except(file2.Keys)
    Dim values = From key In keysInList1ThatAreNotInList2 Select file1(key)
    Dim str = String.Join(vbCrLf, values)

    txtResults.Text = ("IDs should not be in list: " & str)

End Sub
4

2 回答 2

0

当您只处理一个字段时,不确定为什么要使用字典。每个值都有相同的键,字典必须有唯一的键。一个简单的 List(Of String) 应该很好用:

Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    Dim file1 As New List(Of String)
    Dim file2 As New List(Of String)
    For Each line As String In System.IO.File.ReadAllLines(TEST1)
        If line.StartsWith("OP-ID:") Then
            Dim temp As String = line.Split(" "c)(1)
            If Not file1.Contains(temp) Then
                file1.Add(temp)
            End If
        End If
    Next
    For Each line As String In System.IO.File.ReadAllLines(TEST2)
        If line.StartsWith("OP-ID:") Then
            Dim temp As String = line.Split(" "c)(1)
            If Not file2.Contains(temp) Then
                file2.Add(temp)
            End If
        End If
    Next
    Dim keysInList1ThatAreNotInList2() As String = file1.Except(file2).ToArray
    Dim str = String.Join(vbCrLf, keysInList1ThatAreNotInList2)
    txtResults.Text = ("IDs should not be in list: " & vbCrLf & str)
End Sub
于 2013-11-11T17:22:28.033 回答
0

您的示例列表没有任何逗号,并且从问题中不清楚它们是否存在。

如果没有逗号

如果没有重复项TEST1(或者您不关心它们):

Dim hs As New Hashset(Of String)(File.ReadLines(TEST1))
hs.ExceptWith(File.ReadLines(TEST2))
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, hs)}"

如果重复项很重要:

Dim hs As New Hashset(Of String)(File.ReadLines(TEST2))
Dim notFound = File.ReadLines(TEST1).Where(Function(x) Not hs.Contains(x)).ToList
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"

如果有逗号

(共享代码)

Dim keyParser = Function(x As String)
        Dim indexOf = s.IndexOf(
        If indexOf = -1 Then Return x
        Return Left(x, indexOf)
    End Function
Dim list2Keys = New Hashset(Of String)(File.ReadLines(TEST2).Select(keyParser))

如果 中没有重复的键TEST1

Dim list1 = File.ReadLines(TEST1).ToDictionary(keyParser)
Dim notFound = list1.Where(Function(kvp) Not list2Keys.Contains(kvp.Key)).Select(Function(kvp) kvp.Value)
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"

如果 中有重复的键TEST1,但它们无关紧要:

Dim list1 = File.ReadLines(TEST1).ToLookup(keyParser)
Dim notFound = list1.Where(Function(grp) Not list2Keys.Contains(grp.Key)).Select(Function(grp) grp.First)
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"

如果重复的键TEST1很重要:

Dim list1 = File.ReadLines(TEST1).ToLookup(keyParser)
Dim notFound = list1.Where(Function(grp) Not list2Keys.Contains(grp.Key)).SelectMany(Function(grp) grp)
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"
于 2016-11-02T11:39:34.023 回答