如何在VB.net中使用Dictionary? [英] How to use Dictionary in VB.net?
问题描述
我已经编写了此功能,可以自动将字符串数组中的不同值的性别正确地校正为M或F.它工作正常,但我的经理告诉我使用字典,他说字典效率更高.但是我不知道.任何人都想帮助我了解如何做到这一点?谢谢.
公共功能AutoGender(ByVal dt作为数据表)作为数据表性别暗淡为String ="Dim Mkeywords()As String = {男孩",男孩",男性",男人","m",男人",男人"}Dim Fkeywords()As String = {女孩",女孩",女性",女人","f",妇女",小鸡"}昏暗的行作为DataRow对于dt.Rows中的每一行如果Mkeywords.Contains(row("Gender").ToString.ToLower)然后性别="M"row("Gender")=性别ElseIf Fkeywords.Contains(row("Gender").ToString.ToLower)然后性别="F"row("Gender")=性别万一下一个返回dt结束功能
下面是一个示例,您可以如何实现 Dictionary(Of String,String)
来查找此同义词是否已知:
作为字典(字符串,字符串)=的新字典(字符串,字符串)共享的GenderSynonyms{{"boy","M"},{"boys","M"},{"male","M"},{"man","M"},{"m","M"},{"men","M"},{"guy","M"},{"girl","F"},{"girls","F"},{"female","F"},{"woman","F"},{"f","F"},{妇女","F"},{小鸡","F"}}公共功能AutoGender(ByVal dt作为数据表)作为数据表如果dt.Columns.Contains("Gender")然后对于每行作为dt.Rows中的DataRow昏暗的oldGender = row.Field(Of String)("Gender").ToLower将newGender变暗为String = String.Empty如果GenderSynonyms.TryGetValue(oldGender,newGender)然后row.SetField("Gender",newGender)万一下一个万一返回dt结束功能
请注意,我已经使用集合初始化程序来填充词典,这是使用文字初始化集合的便捷方法.您也可以使用 添加
方法.>
编辑:另一种更为简洁的方法是使用两个 HashSet(Of String)
,一个用于男性同义词,一个用于女性同义词:
共享的maleSynonyms作为新的HashSet(Of String)来自{男孩",男孩",男性",男人","m",男人",男人"}共享femaleSynonyms作为新的HashSet(Of String)From{女孩",女孩",女性",女人","f",妇女",小鸡"}公共功能AutoGender(ByVal dt作为数据表)作为数据表如果dt.Columns.Contains("Gender")然后对于每行作为dt.Rows中的DataRow昏暗的oldGender = row.Field(Of String)("Gender").ToLower将newGender变暗为String = String.Empty如果maleSynonyms.Contains(oldGender)然后row.SetField("Gender","M")否则如果是女性同义词.包含(oldGender)然后row.SetField("Gender","F")万一下一个万一返回dt结束功能
HashSet
也必须是唯一的,因此它不能包含重复的 Strings
(例如 Dictionary
中的键),但不是键值对,但只有一组.
I have written this function to auto correct gender to M or F from different values in a string array. It works fine but my manager told me to use Dictionary which he said is more efficient. But I have no idea. Anyone like to help me to understand how this can be done ? Thanks.
Public Function AutoGender(ByVal dt As DataTable) As DataTable
Dim Gender As String = ""
Dim Mkeywords() As String = {"boy", "boys", "male", "man", "m", "men", "guy"}
Dim Fkeywords() As String = {"girl", "girls", "female", "woman", "f", "women", "chick"}
Dim row As DataRow
For Each row In dt.Rows
If Mkeywords.Contains(row("Gender").ToString.ToLower) Then
Gender = "M"
row("Gender") = Gender
ElseIf Fkeywords.Contains(row("Gender").ToString.ToLower) Then
Gender = "F"
row("Gender") = Gender
End If
Next
Return dt
End Function
Here is an example how you could implement the Dictionary(Of String, String)
to lookup whether this synonym is known or not:
Shared GenderSynonyms As Dictionary(Of String, String) = New Dictionary(Of String, String) From
{{"boy", "M"}, {"boys", "M"}, {"male", "M"}, {"man", "M"}, {"m", "M"}, {"men", "M"}, {"guy", "M"},
{"girl", "F"}, {"girls", "F"}, {"female", "F"}, {"woman", "F"}, {"f", "F"}, {"women", "F"}, {"chick", "F"}}
Public Function AutoGender(ByVal dt As DataTable) As DataTable
If dt.Columns.Contains("Gender") Then
For Each row As DataRow In dt.Rows
Dim oldGender = row.Field(Of String)("Gender").ToLower
Dim newGender As String = String.Empty
If GenderSynonyms.TryGetValue(oldGender, newGender) Then
row.SetField("Gender", newGender)
End If
Next
End If
Return dt
End Function
Note that i've used the collection initializer to fill the Dictionary that is a convenient way to use literals to initialize collections. You could also use the Add
method.
Edit: Just another approach that might be more concise is using two HashSet(Of String)
, one for the male synonyms and one for the female:
Shared maleSynonyms As New HashSet(Of String) From
{"boy", "boys", "male", "man", "m", "men", "guy"}
Shared femaleSynonyms As New HashSet(Of String) From
{"girl", "girls", "female", "woman", "f", "women", "chick"}
Public Function AutoGender(ByVal dt As DataTable) As DataTable
If dt.Columns.Contains("Gender") Then
For Each row As DataRow In dt.Rows
Dim oldGender = row.Field(Of String)("Gender").ToLower
Dim newGender As String = String.Empty
If maleSynonyms.Contains(oldGender) Then
row.SetField("Gender", "M")
ElseIf femaleSynonyms.Contains(oldGender) Then
row.SetField("Gender", "F")
End If
Next
End If
Return dt
End Function
A HashSet
must also be unique, so it cannot contain duplicate Strings
(like the key in the Dictionary
), but it's not a key-value pair but only a set.
这篇关于如何在VB.net中使用Dictionary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!