Analogowy domyslny Python?
Czy istnieje analog .NET Pythonadefaultdict
? Uważam za przydatne napisanie krótkiego kodu, np. liczenie częstotliwości:
>>> 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})
Idealnie w C # mógłbym napisać:
var frequencies = new DefaultDictionary<string,int>(() => 0);
foreach(string word in words)
{
frequencies[word] += 1
}