2012-06-21 66 views
3

我已經寫了這個函數來自動將字符串數組中的不同值修改爲M或F。它工作正常,但我的經理告訴我使用他說的效率更高的詞典。但我不知道。任何人都想幫助我理解如何做到這一點?謝謝。如何在VB.net中使用字典?

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 
+0

也許這可以幫助http://www.dotnetperls.com/dictionary-vbnet – Bishan

回答

7

下面是一個例子,你如何可以實現Dictionary(Of String, String)查找這個代名詞是已知還是不:

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 

請注意,我用的collection initializer填補Dictionary那就是使用一種方便的方法文字初始化集合。您也可以使用Add method

編輯:只是另一種方法可能是使用兩個HashSet(Of String),一個爲男性同義詞和一個針對女性更簡潔:

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 

一個HashSet也必須是唯一的,所以它不能包含重複Strings(如Dictionary中的密鑰),但它不是鍵值對,而只是一組值。

+1

我寫了這個EXACT相同的代碼,但當我有錯誤檢查它時,你擊敗了我! – stevehipwell

+0

非常感謝Tim ......你是最棒的! – lawphotog

3

只要雙方你的陣列改變字典,做的ContainsKey代替Contains

Dim Mkeywords = New Dictionary(Of String, String) From 
    {{"boy", ""}, {"boys", ""}, {"male", ""}, {"man", ""}, {"m", ""}, {"men", ""}, {"guy", ""}} 

(和跟風的女性)

但是,正如你可能已經注意到了,我把所有的空字符串。這是因爲詞典和鍵都有值,但由於我們沒有使用這些值,所以我將它們設爲空字符串。要使用相同的O(1)查找,但要避免所有無關值,可以使用類似方式使用HashSet

所有您需要更改的,就像我說的,用ContainsKey(或HashSet,如果你走這條路線,它仍然只是Contains):

If Mkeywords.ContainsKey(row("Gender").ToString.ToLower) Then 

最後一點:這隻會是「如果數據開始大幅度增長,那麼效率會更高。就目前而言,只有少數元素,使用字典的速度可能會更慢。