如何组合或加入最近的项目类似于例如
Dim _lstOfFontsList As New List(Of String)
From {"Arial", "Arial", "Arial", "GEORGIA", "Arial",
"Arial", "GEORGIA", "GEORGIA",
"ABOlD", "ABOlD", "Arial"}
_lstOfFontsList
本身有11个值,这里0,1,2个项目是相似的,5,6和7,8和9,10个项目在这里类似我想要对类似的项目进行分组以便尝试使用groupby功能如下所示
Dim Cntdd = _lstOfFontsList.GroupBy(Function(S) S).ToDictionary
(Function(a) a.Key, Function(a) a.Count)
但输出如下
[Arial, 6]
[GEORGIA, 3]
[ABOlD, 2]
但预期输出
Arial = 3
Georgia =1
Arial=2
Georgia=2
Abold=2
Arial=1
如何组合或分组或加入最接近(下一个)字符串列表中的类似项目
更新: 我试图循环条件以获得上面的值,但预期的结果将不会来到这里是代码
Dim _fntCnt% = 0
Dim _finalItms As New List(Of String)
Dim _finalFntName$ = ""
Dim LASTITEMS As New List(Of String)
For i = 0 To _lstOfFontsList.Count - 1
If i = 0 Then
_fntCnt += 1
_finalItms.Add(_lstOfFontsList(i) & "->" & _fntCnt)
_finalFntName$ = _lstOfFontsList(i)
End If
Trace.WriteLine(i)
If i <> _lstOfFontsList.Count - 1 Then
If _lstOfFontsList(i) = _lstOfFontsList(i + 1) Then
_fntCnt += 1
_finalItms.Add(_lstOfFontsList(i) & "->" & _fntCnt)
Else
For Each _itmm In _finalItms
LASTITEMS.Add(_itmm & " " & _finalFntName.Count)
Next
_finalItms.Clear()
End If
End If
Next
请任何人帮忙
答案 0 :(得分:1)
我确信必须有更简单的解决方案,但这也表明了这个想法背后的工作。正如JM所说,你需要一个循环并检查项目是否还有更多。在这种情况下,您不能使用字典,因为您需要唯一值,因此我们必须使用列表(对)。
希望这有意义并且有所帮助!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim _lstOfFontsList As New List(Of String) _
From {"Arial", "Arial", "Arial", "GEORGIA", "Arial",
"Arial", "GEORGIA", "GEORGIA",
"ABOlD", "ABOlD", "Arial"}
Dim Output As List(Of KeyValuePair(Of String, Integer)) = New List(Of KeyValuePair(Of String, Integer))
Dim Count As Integer = 1
For i = 0 To _lstOfFontsList.Count - 1
Dim Itm As String = _lstOfFontsList(i)
'Erro trap as the last i will error as there is no next item at the end of the list
Dim Nxt_Itm As String = Nothing
If i + 1 < _lstOfFontsList.Count Then Nxt_Itm = _lstOfFontsList(i + 1)
If Itm = Nxt_Itm Then
'Same as next - so we +1 to the count
Count += 1
Else
'Not same, so we need to add to the collection and reset the count to 1
Output.Add(New KeyValuePair(Of String, Integer)(Itm, Count))
Count = 1
End If
Next
For Each pair In Output
Debug.WriteLine(pair.Key & " | " & pair.Value)
'Will write "Arial | 3" for example...
Next
End Sub
H个 鸡