In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Python collections module how to use, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
The collections module is an addictive module without knowing it. Because it provides several very convenient data structures and methods, it is particularly useful in some cases. Today, I will give you a summary of the two methods of OrderDict and Counter, which I often use in peacetime. I hope you can like it, too. Then there are some methods, such as deque,namedtuple,defaultdict, etc., which need to be mastered when you understand that the key point is to use it skillfully and flexibly to your specific tasks.
1. OrderDict
# Example 1
Dict_1 = dict ()
Dict_1 ['A'] = "I love machine learning" .split ("")
Dict_1 ['B'] = "I love life too" .split ("")
Dict_1 ['C'] = "which you like and which you like" .split ("")
Print ("first output dictionary: {}" .format (dict_1))
For x, y in dict_1.items ():
Print ("then traverse the dictionary: {}" .format ((x, y)
Print ("Type of dict_1: {}" .format (type (dict_1)
"
First output dictionary: {'you',: [' Illustrated, 'love',' life', 'too'],' which',: ['which',' you', 'like',' and', 'which',' you', 'dictionary],' Agar: ['Illustrated,' love', 'machine',' dictionary]}
Then iterate through the dictionary: ('love', [' love', 'life',' dictionary])
Then iterate through the dictionary: ('which',' you', 'like',' and', 'which',' you', 'like'])
Then iterate through the dictionary: ('Agar, [' love', 'machine',' learning'])
Type of dict_1:
"
If you execute the above program many times, you will find that the order of each output is different, which is the disorder of the dictionary.
From collections import OrderedDict
Dict_2 = OrderedDict ()
Dict_2 ['A'] = list ("machine")
Dict_2 ['B'] = list ("learning")
Dict_2 ['C'] = list ("math")
Print ("first output dictionary: {}" .format (dict_2))
For k, v in dict_2.items ():
Print ("then traverse the dictionary: {}" .format ((k, v)
Print ("Type of dict_2: {}" .format (type (dict_2)
"
First output the dictionary: OrderedDict ([('Aging, [' masked, 'axed,' cached, 'hacked,' ified, 'nasty,' e']), (['lumped,' eyed, 'axed,' rusted, 'nicked,' nasty,'g']), (['masked,' axed, 'tasked,' h']])
Then iterate through the dictionary: ('Agar, [' masked, 'axed,' cached, 'hagged,' ified, 'nasty,' e'])
Then iterate through the dictionary: ('baked, [' lumped, 'estranged,' ajar, 'ringing,' nasty, 'iTunes,' nasty,'g'])
Then iterate through the dictionary: ('clocked, [' masked, 'axed,' tweeted,'h'])
Type of dict_2:
"
Execute the above program many times and you will find that each output key is in the A, B, C order.
2. Counter
Counter is a statistical method that can count frequency, such as word frequency, etc. Look at a chestnut.
# Example 2
From collections import Counter
List_1 = list ("machine learning and math")
Fre = Counter (list_1)
Print (fre)
"
Counter ({'nails: 4,' asides: 4,': 3, 'masks: 2,' eBay: 2, 'iTunes: 2,' hints: 2, 'lags: 1,' cations: 1, 'gathers: 1,' tweets: 1, 'dudes: 1,' ritual: 1})
"
For f, v in fre.most_common (nasty 5):
Print ("top 5 occurrences: {}" .format ((f, v)
"
5 of the most frequent occurrences: ('nasty, 4)
5 of the most frequent occurrences: ('asides, 4)
5 of the most frequent occurrences: (', 3)
5 of the most frequent occurrences: ('hackers, 2)
5 of the most frequent occurrences: ('eBay, 2)
"
Then take Little Chestnut for word frequency statistics.
Word frequency statistics is that for a paragraph of text, first do word segmentation, and then count the number of times each word appears. Use the jieba participle to do it today. The sentence is extracted from Zhu Ziqing's "Moonlight in the Lotus Pond".
Go directly to the code
Import jieba
Test_str = "I was the only one on the road, walking with my hands behind my back. This piece of heaven and earth seems to be mine; I also seem to be beyond my usual self,"\
"in another world. I love lively and calm; I like to live in groups and I love to be alone. Like tonight, I'm here alone.
"under the boundless moon, you can think of anything, you don't have to think about anything, and you feel like a free man. what you must do during the day.
"you can ignore it now. This is the beauty of solitude, and I'll take advantage of the boundless lotus moonlight."
Test_str_cut = jieba.lcut (test_str)
Word_freq = Counter (test_str_cut)
For w, f in word_freq.most_common (10):
Print (w, f)
"
7%
I am 5
These four
Love 4
3.
Also 3
A 3.
All three
Solitude 2
Is it helpful for you to read the above content? if you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.