2017-07-22 40 views
-2

一個鍵我不添加如何字典包含多個項目到另一個字典作爲值字典作爲值到另一個詞典

Dictionary<string,Dictionary<string,double>> proptest=new Dictionary<string,Dictionary<string,double>> 
if (proptest.ContainsKey(filenametest)) 
       { 

        proptest[filenametest].Add(filenametraining, NB) ; 
       } 
       else 
       { 
        proptest.Add(filenametest, new Dictionary<string, double> { { filenametraining, NB } }); 
       } 

結果爲: {[10171.txt,System.Collections.Generic。我想要的結果是: {[10171.txt:(cat10 0.4)(cat3 0.6)]}

+2

[多值字典的可能的複製](https://stackoverflow.com/questions/569903/multi-value-dictionary),你也可以用[Tuple](https://msdn.microsoft.com/de-de/library/system.tuple v = vs.110)的.aspx)。 – jAC

+0

什麼是問題?你可以說得更詳細點嗎? – eocron

+0

如何將字典作爲值添加到另一個字典中的一個鍵? – Jojo

回答

0

您可以添加一個現有的字典,像這樣:

// The 'outer' dictionary: 
var myDictionary = new Dictionary<string, Dictionary<string, double>>(); 

// an existing 'inner' dictionary: 
var innerDictionary1 = new Dictionary<string, double> { 
    { "inner1.1", 6 }, { "inner1.2", 4.5 } 
}; 

// add the inner to the outer: 
myDictionary.Add("outer1", innerDictionary1); 

要添加一個「內部」字典在線:

myDictionary.Add("outer2", 
    new Dictionary<string, double> { 
     { "inner2.1", 3.48748}, { "inner2.2", 25 }, { "inner2.3", 0 } 
    }); 

另外,如果你想將值添加到內部字典,你可以這樣做:

myDictionary["outer2"].Add("inner2.4", 3.56); 
相關問題