Esiste un analogo .NET di Python defaultdict
? Trovo utile scrivere un codice breve, ad es. frequenze di conteggio:Defaultdict dell'analogue of Python?
>>> words = "to be or not to be".split()
>>> print words
['to', 'be', 'or', 'not', 'to', 'be']
>>> from collections import defaultdict
>>> frequencies = defaultdict(int)
>>> for word in words:
... frequencies[word] += 1
...
>>> print frequencies
defaultdict(<type 'int'>, {'not': 1, 'to': 2, 'or': 1, 'be': 2})
Quindi, idealmente in C# ho potuto scrivere:
var frequencies = new DefaultDictionary<string,int>(() => 0);
foreach(string word in words)
{
frequencies[word] += 1
}
Cosa significa il 'defaultdict' fare ? Non ho familiarità con Python. Modifica: è solo un dizionario che ha un valore predefinito per le chiavi. È possibile sottoclasse 'Dictionary' e implementare la funzionalità. – Romoku
La risposta di Jon Skeet può aiutare: http://stackoverflow.com/a/2601501/1786606. – Vladimir