gpt4 book ai didi

.net - 字典类型的问题,如果给定一个特定的文本然后得到等效的字典键或字典值?

转载 作者:行者123 更新时间:2023-12-02 02:00:32 26 4
gpt4 key购买 nike

我有一个字典(字符串,字符串),我使用字典值设置目录路径,并使用键为每个值设置描述。

例如:

Value of a random item of my dictionary: "C:\Test"
Key name of that item = "Test folder"

好吧,现在在我的应用程序中我可以选择是显示描述还是完整的目录路径,这就是问题所在...

一些图片来理解它:

1 - 在这里我存储/管理我的字典的项目:

enter image description here

2 - 我可以切换此复选框以在组合框中显示字典键或字典值(您将在下一张图片中看到):

enter image description here

3 - 描述复选框被选中,然后描述显示在组合框中,当我添加一个项目时,它会添加选定的组合框。文本(可以是描述或完整路径)

enter image description here

4 - 现在我取消选中描述复选框,我想要的是更改 ListView 的描述,就像我将组合框的描述更改为等效的完整路径一样(但我所做的是更改组合框名称是重新加载字典,因为它是按字母顺序排序的,所以......我不知道如何对 ListView 子项做同样的事情)

enter image description here

我想提醒您,listview 子项文本可以是描述,也可以是完整路径,所以我需要在这两种选择中改变思路。

...这是我的代码:

(请阅读评论)

    ' First of all
' "Item.SubItems(2).Text" can be the description or it can be the full path, so I need to do it with both alternatives

If ListView_Monitor.Items.Count <> 0 Then

For Each Item As ListViewItem In ListView_Monitor.Items

If ShowDescriptions Then ' Showdescription is a boolean var to show descriptions or full paths

' Description is stored in the "Dictionary.Key"
' I don't know how to get the key name of the item

' Item.SubItems(2).Text = Directories_SendTo.keys ... ...
' CType(Item.SubItems(2).Text, Directories_SendTo... ...)

ElseIf Not ShowDescriptions Then ' Don't show descriptions, I will show fullpaths

' Fullpath is stored in the "Dictionary.Value"

' Remember that "Item.SubItems(2).Text" can be the description or the fullpath
' So if "Item.SubItems(2).Text" is the description then this piece of code works, 'cause the dictionary keyname is the same as the description name
Item.SubItems(2).Text = Directories_SendTo(Item.SubItems(2).Text)

' Here I need an alternative if "Item.SubItems(2).Text" is the directory path and not the description
End If

Next

End If

UPDATE:

解决方案(目前)...

我的问题是我是否可以改进这段代码(也许不要在 dict 中循环):

        If ListView_Monitor.Items.Count <> 0 Then

For Each Item As ListViewItem In ListView_Monitor.Items

If ShowDescriptions Then ' Show descriptions

For Each value In Directories_SendTo.Values
If value = Item.SubItems(2).Text Then
Item.SubItems(2).Text = FindKeyByValue(Directories_SendTo, Item.SubItems(2).Text)
End If
Next

ElseIf Not ShowDescriptions Then ' Show fullpaths

For Each key In Directories_SendTo.Keys
If key = Item.SubItems(2).Text Then
Item.SubItems(2).Text = Directories_SendTo(key)
End If
Next

End If

Next

End If

Public Function FindKeyByValue(Of TKey, TValue)(dictionary As Dictionary(Of TKey, TValue), value As TValue) As TKey

For Each pair As KeyValuePair(Of TKey, TValue) In dictionary
If value.Equals(pair.Value) Then Return pair.Key
Next

' Throw New Exception("The value is not found in the dictionary.")
Return Nothing
End Function

最佳答案

要回答你的第二个问题,关于如何改进你正在使用的循环,它可以得到显着改进。目前,如果您在 ListView 中有 100 个项目,在目录中有 10 个项目,您的代码将循环至少 1000 次,最多可能循环 10 次,具体取决于目录中使用的值以及是否使用 ShowDescriptions被标记。

我们可以在进入赋值循环之前通过适当的键构建一个字典,将直接循环减少到 110 次:

    If ListView_Monitor.Items.Count <> 0 Then
Dim DirectoriesByCurrentKey As New Dictionary(Of String, String)

If ShowDescriptions Then
' If we are showing the descriptions, add each of the items to the new collection, keyed by the value
For Each key In Directories_SendTo.Keys
Dim Description As String
Description = Directories_SendTo(key)
' We don't know if description is unique, so make sure that we don't get a runtime error if we
' try to add the same description multiple time
If Not DirectoriesByCurrentKey.ContainsKey(Description) Then
DirectoriesByCurrentKey.Add(Description, key)
End If
Next
Else
' Just use the current collection
DirectoriesByCurrentKey = Directories_SendTo
End If

For Each Item As ListViewItem In ListView_Monitor.Items
Dim sDescription As String = ""
' Try to find the current description using the current list item description
If DirectoriesByCurrentKey.TryGetValue(Item.SubItems(2).Text, sDescription) Then
' If we found the entry, change the description to what we found
Item.SubItems(2).Text = sDescription
End If
Next
End If

请注意,因为我们不知道 Description 是否唯一,所以我们需要在添加它之前测试它是否存在于新词典中。这将导致与您当前相同的行为,因为您的 FindKeyByValue 在第一次匹配时停止。

关于.net - 字典类型的问题,如果给定一个特定的文本然后得到等效的字典键或字典值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17459718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com