编辑:另一种更为简洁的方法是使用两个 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 dtEnd FunctionNote 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 dtEnd FunctionA 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-30 10:31