2017-07-22 12 views
-2

ich nicht, wie hinzugefügt Wörterbuch mehrere Artikel zu einem anderen Wörterbuch als WertWörterbuch als Wert zu einem Schlüssel in einem anderen Wörterbuch

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 } }); 
       } 

das Ergebnis enthalten ist: {[10171.txt, System.Collections.Generic. Dictionary`2 [System.String, System.Double]]} während ich will Ergebnis ist: {[10171.txt: (cat10 0,4) (cat3 0,6)]}

+2

Mögliches Duplikat [Multi Wert Wörterbuch ] (https://stackoverflow.com/questions/569903/multi-value-dictionary), Sie könnten auch mit [Tuple] (https://msdn.microsoft.com/de-de/library/system.tuple (v = vs.110) .aspx). – jAC

+0

Was ist ein Problem? Kannst du genauer sein? – eocron

+0

Wie kann ich einem Schlüssel in einem anderen Wörterbuch ein Wörterbuch als Wert hinzufügen? – Jojo

Antwort

0

Sie können ein vorhandenes Wörterbuch wie folgt hinzufügen :

// 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); 

Um eine ‚innere‘ Wörterbuch Inline hinzufügen:

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

Auch, wenn Sie zu einem inneren Wörterbuch Werten hinzufügen möchten, können Sie dies tun:

myDictionary["outer2"].Add("inner2.4", 3.56); 
Verwandte Themen