In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how python uses counters to count elements. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Use counters for element counting
When we have multiple items in a list, tuple, or string (for example, multiple characters), we often want to calculate how many elements there are in each item. To do this, you can write some tedious code for this feature.
Words = ['an',' boy',' girl', 'an',' boy',' dog', 'cat',' Dog', 'CAT',' an','GIRL', 'AN',' dog', 'cat',' cat', 'bag',' BAG', 'BOY',' boy', 'an']. Unique_words = {x.lower () for x in set (words)}. For word in unique_words:... Print (f "* Count of {word}: {words.count (word)}")... * Count of cat: 3 * Count of bag: 1 * Count of boy: 3 * Count of dog: 2 * Count of an: 5 * Count of girl: 1
As shown above, we must first create a collection that contains only unique words. Then we iterate over the word set and use the count () method to find out the occurrence of each word. However, there is a better way to use the Counter class to accomplish this counting task.
From collections import Counter. Word_counter = Counter (x.lower () for x in words)... Print ("Word Counts:", word_counter)... Word Counts: Counter ({'an': 5,' boy': 4, 'cat': 4,' dog': 3, 'girl': 2,' bag': 2})
This counter class is available in the collections module. To use this class, we just need to create a generator:,x.lower () for x in words where each item will be counted. As we can see, the Counter object is a dict-like mapping object, where each key corresponds to a unique item in the word list, and the value is the count of those items.
In addition, if we are interested in finding the items that appear most frequently in the word list, we can use the most_common () method of the Counter object. The following code shows this usage. We only need to specify an integer (N) to find the N items that are most frequent in the list. Incidentally, this object will also be used with other sequence data, such as strings and tuples.
> > # Find out the most common item... Print ("Most Frequent:", word_counter.most_common (1)) Most Frequent: [('an', 5)] > > # Find out the most common 2 items. Print ("Most Frequent:", word_counter.most_common (2)) Most Frequent: [('an', 5), (' boy', 4)] Thank you for reading! This is the end of this article on "how to use counters for element counting in python". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.