我想比较两个字符串列表:
Dim ListV1 = New List(Of List(Of String))
Dim ListV2 = New List(Of List(Of String))
Dim ListResult = New List(Of List(Of String))
Fil(ListV1)
Fil(ListV2)
ListResult = ***Compare***( ListV1 ,ListV2 )
我可以使用哪些代码来生成此比较?
答案 0 :(得分:0)
这是我可以做的最好的假设比较意义等价值,如果子列表之间存在差异,列表应该包含所有一个子列表。
Enum Side
Left
Right
End Enum
Function Compare2DLists(list1 As List(Of List(Of String)), list2 As List(Of List(Of String)), takeSide As Side) As List(Of List(Of String))
Dim newList As New List(Of List(Of String))
For i = 0 To list1.Count - 1
Dim areEqual As Boolean = True
For j = 0 To list1.Count - 1
If Not list1(i)(j) = list2(i)(j) Then
areEqual = False
End If
Next
If areEqual = False Then
Select Case takeSide
Case Side.Left
newList.Add(list1(i))
Case Side.Right
newList.Add(list2(i))
End Select
Else
newList.Add(list1(i))
End If
Next
Return newList
End Function
这假设list1与list2的大小相同,如果list1是4x4则list2是4x4
答案 1 :(得分:0)
我将在这里做出一些假设,在你的情况下这可能是不正确的。你真的需要弄清楚这个比较应该如何运作的一些观点:
List
(而不是Hashset
),我认为订单很重要 - {"a","b"}
不等同于{"b","a"}
。List(Of String)
比另一个Nothing
长,则相应的结果列表将与较长的列表一样长,但会填充List(Of List(Of String))
值的差异。Nothing
来存储结果,我假设您要返回相应位置的等值值;非等效值将在相应的结果中显示Imports System.Runtime.CompilerServices
Module ListFunctions
Public Function MaxLength(Of T)(l1 As List(Of T), l2 As List(Of T)) As T
Dim l1Length = If(l1?.Count,0)
Dim l2Length = If(l2?.Count,0)
Return Math.Max(l1Length, l2Length)
End Function
<Extension()> Public Function TryGet(Of T)(l As List(Of T), idx As Integer) As T
If If(l?.Count, 0) <= idx Then Return Nothing
Return l(idx)
End Function
End Module
。我建议添加以下两种扩展方法:
Dim listComparer = Function(l1 As List(Of String), l2 As List(Of String))
Return Enumerable.Range(0, MaxLength(l1,l2)).Select(Function(i)
Dim s1 = l1.TryGet(i)
Dim s2 = l2.TryGet(i)
If s1 = s2 Then Return s1
Return Nothing
End Function).ToList
End Function
Dim listsComparer = Function(ll1 As List(Of List(Of String)), ll2 As List(Of List(Of String)))
Return Enumerable.Range(0, MaxLength(ll1,ll2)).Select(Function(i)
Dim l1 = ll1.TryGet(i)
Dim l2 = ll2.TryGet(i)
Return listComparer(l1,l2)
End Function).ToList
End Function
Dim ListResult = listsComparer(ListV1, ListV2)
然后,在您的代码块中:
if (strcmp ($res, "VERIFIED") == 0) {