परिचय
जब स्ट्रिंग शॉर्टहैंड का उपयोग करके शब्दों की आवृत्ति खोजनी हो, तो डिक्शनरी कॉम्प्रिहेंशन का उपयोग किया जा सकता है।
उदाहरण
my_str = 'Hi there Will, how are you Will, Will you say Hi to me'
print("The string is : ")
print(my_str)
my_result = {key: my_str.count(key) for key in my_str.split()}
print("The word frequency is : ")
print(my_result)
आउटपुट
The string is : Hi there Will, how are you Will, Will you say Hi to me
The word frequency is : {'Hi': 2, 'there': 1, 'Will,': 2, 'how': 1, 'are': 1, 'you': 2, 'Will': 3, 'say': 1, 'to': 1, 'me': 1}
व्याख्या
- एक स्ट्रिंग परिभाषित की जाती है, और कंसोल पर प्रदर्शित की जाती है।
- डिक्शनरी कॉम्प्रिहेंशन का उपयोग करके स्ट्रिंग पर पुनरावृत्ति की जाती है, और इसे स्पेस के आधार पर विभाजित किया जाता है।
- 'key' की गिनती निर्धारित की जाती है, और एक वेरिएबल में निर्दिष्ट की जाती है।
- यह वेरिएबल कंसोल पर आउटपुट के रूप में प्रदर्शित होता है।