如何在使用 List 和 Dictionary 之间获得第一个最快的事件
How to get the first soonest occurrence between a use of a List and Dictionary
列表未排序,第一第二和第三的值 (1,2,3)
列表指示值的顺序是第一、第二或第三。
行就是我所说的字典的键
字典的值是列表中使用的值
(因为Row:200有最小的最快Value:2)(基于newList)2在3之前,
Row:100 中的 Value:3 晚于 Value:2,这就是为什么输出应该是 Row:200.
Row:200解析后,下一个输出是Row:300,Row:100是最后一个输出值,但我只需要输出一个结果,而不是全部输出3个。
键是行
值是值
输出是字典的键
Public Module Program
Public Sub Main(args() As String)
'This kinda works I guess
Dim newList2 As New List(Of Byte)({1, 2, 3})
Dim ListOfValues2 As New Dictionary(Of Integer, Byte)
ListOfValues2.Add(100, 3)
ListOfValues2.Add(200, 2)
ListOfValues2.Add(300, 1)
Dim firstToProcessRow As Integer = 0
For Each value In newList2
For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues2
If ListOfValues2.ContainsKey(Row.Value) AndAlso ListOfValues2(Row.Value) = value OrElse Row.Key = 0 Then
firstToProcessRow = Row.Key
GoTo exitFor
End If
Next
Next
exitFor:
Debug.WriteLine(firstToProcessRow)
'Output is 100 when it should be 200.
End Sub
End Module
使用评分系统解决
基本上这是一类不能一蹴而就的问题。您需要存储一些临时结果然后进行处理。
“您似乎在试图找出哪个值具有最低 'score',其中分数基于每个数组中公共值所在位置的索引”
这是解决方案
If newList.LongCount > 0 Then
Dim scores As Integer() = New Integer(ListOfValues.Count - 1) {}
Dim scoreCounter As Integer = 0
Dim highestScore As Integer = -1
For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues
Dim key As Integer = Array.IndexOf(newList.ToArray(), Row.Value)
scores(scoreCounter) = key
scoreCounter += 1
If key > highestScore Then highestScore = key
Next
Debug.WriteLine("scores")
Debug.WriteLine("Highest Score: " & highestScore)
If highestScore <> -1 Then
Dim firstLowestValue As Integer = highestScore
scoreCounter = 0
For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues
If scores(scoreCounter) <> -1 AndAlso scores(scoreCounter) < firstLowestValue Then
Debug.WriteLine("Lowest = " & newList(scores(scoreCounter)) & " Lowest Row = " & Row.Key)
Return Row.Key
End If
scoreCounter += 1
Next
firstLowestValue = highestScore
scoreCounter = 0
For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues
If scores(scoreCounter) <> -1 AndAlso scores(scoreCounter) <= firstLowestValue Then
Debug.WriteLine("Lowest2 = " & newList(scores(scoreCounter)) & " Lowest Row = " & Row.Key)
Return Row.Key
End If
scoreCounter += 1
Next
If firstLowestValue = highestScore Then Return ListOfValues.Keys.Min()
End If
Else
Return CurrentRow
End If
列表未排序,第一第二和第三的值 (1,2,3)
列表指示值的顺序是第一、第二或第三。
行就是我所说的字典的键
字典的值是列表中使用的值
(因为Row:200有最小的最快Value:2)(基于newList)2在3之前,
Row:100 中的 Value:3 晚于 Value:2,这就是为什么输出应该是 Row:200.
Row:200解析后,下一个输出是Row:300,Row:100是最后一个输出值,但我只需要输出一个结果,而不是全部输出3个。
键是行
值是值
输出是字典的键
Public Module Program
Public Sub Main(args() As String)
'This kinda works I guess
Dim newList2 As New List(Of Byte)({1, 2, 3})
Dim ListOfValues2 As New Dictionary(Of Integer, Byte)
ListOfValues2.Add(100, 3)
ListOfValues2.Add(200, 2)
ListOfValues2.Add(300, 1)
Dim firstToProcessRow As Integer = 0
For Each value In newList2
For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues2
If ListOfValues2.ContainsKey(Row.Value) AndAlso ListOfValues2(Row.Value) = value OrElse Row.Key = 0 Then
firstToProcessRow = Row.Key
GoTo exitFor
End If
Next
Next
exitFor:
Debug.WriteLine(firstToProcessRow)
'Output is 100 when it should be 200.
End Sub
End Module
使用评分系统解决
基本上这是一类不能一蹴而就的问题。您需要存储一些临时结果然后进行处理。
“您似乎在试图找出哪个值具有最低 'score',其中分数基于每个数组中公共值所在位置的索引”
这是解决方案
If newList.LongCount > 0 Then
Dim scores As Integer() = New Integer(ListOfValues.Count - 1) {}
Dim scoreCounter As Integer = 0
Dim highestScore As Integer = -1
For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues
Dim key As Integer = Array.IndexOf(newList.ToArray(), Row.Value)
scores(scoreCounter) = key
scoreCounter += 1
If key > highestScore Then highestScore = key
Next
Debug.WriteLine("scores")
Debug.WriteLine("Highest Score: " & highestScore)
If highestScore <> -1 Then
Dim firstLowestValue As Integer = highestScore
scoreCounter = 0
For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues
If scores(scoreCounter) <> -1 AndAlso scores(scoreCounter) < firstLowestValue Then
Debug.WriteLine("Lowest = " & newList(scores(scoreCounter)) & " Lowest Row = " & Row.Key)
Return Row.Key
End If
scoreCounter += 1
Next
firstLowestValue = highestScore
scoreCounter = 0
For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues
If scores(scoreCounter) <> -1 AndAlso scores(scoreCounter) <= firstLowestValue Then
Debug.WriteLine("Lowest2 = " & newList(scores(scoreCounter)) & " Lowest Row = " & Row.Key)
Return Row.Key
End If
scoreCounter += 1
Next
If firstLowestValue = highestScore Then Return ListOfValues.Keys.Min()
End If
Else
Return CurrentRow
End If