क्या पाइथन के defaultdict
का .NET एनालॉग है? मुझे शॉर्ट कोड लिखना उपयोगी लगता है, उदाहरण के लिए। गिनती आवृत्तियों:पायथन के डिफ़ॉल्ट डिक्शनरी का एनालॉग?
>>> 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})
तो सी # में आदर्श
मैं लिख सकते हैं:
var frequencies = new DefaultDictionary<string,int>(() => 0);
foreach(string word in words)
{
frequencies[word] += 1
}
'defaultdict' क्या करता है ? मैं अजगर से परिचित नहीं हूँ। संपादित करें: यह केवल एक शब्दकोश है जिसमें कुंजी के लिए डिफ़ॉल्ट मान है। आप 'डिक्शनरी' को उपclass कर सकते हैं और कार्यक्षमता को कार्यान्वित कर सकते हैं। – Romoku
जॉन स्कीट का उत्तर मदद कर सकता है: http://stackoverflow.com/a/2601501/1786606। – Vladimir