Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Python count the occurrence frequency of elements in a sequence?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you how Python statistics the frequency of elements in the sequence of the relevant knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

1. How to count the occurrence frequency of elements in a sequence

Actual case:

(1) A random sequence [12, 5, 6, 4, 6, 5, 5, 7,.] Find the three elements with the highest number of occurrences in. How many times do they appear?

(2) according to the word frequency statistics of an English article, we can find the 10 words with the highest frequency. How many times do they appear?

Solution:

Working with collections.Counter objects

Pass the sequence into the constructor of Counter to get the dictionary that the Counter object is the frequency of the element.

The Counter.most_common (n) method gets the list of n elements with the highest frequency.

2. Code demonstration

(1) A random sequence [12, 5, 6, 4, 6, 5, 5, 7,.] Find the three elements with the highest number of occurrences in. How many times do they appear?

From random import randint # uses list parsing to generate a random sequence data = [randint (0,20) for _ in range (30)] print (data) # method 1 the final statistical result must be a dictionary, such as: {2: 5, 4:9}, with each element in data as the key of the dictionary and 0 as the initial value, create such a dictionary''c = dict.fromkeys (data, 0) print (c) # to iterate over data For x in data: C [x] + = 1print (c) # sorts dictionary entries according to the value of the dictionary And truncate the first three elements sort_dict = sorted (c.items (), key=lambda item: item [1], reverse=True) [0:3] print (sort_dict) # method 2:from collections import Counter# directly passes the sequence to the Counter constructor c2 = Counter (data) print (c2) # directly use the object's most_common () method to find the three print with the highest frequency (c2.most_common (3))

(2) according to the word frequency statistics of an English article, we can find the 10 words with the highest frequency. How many times do they appear?

From collections import Counter# imports regular expression module import re # reads the entire file content as a string txt = open ('word.txt'). Read () print (txt) # to count the word frequency, first need to split each word out # use non-alphabetic characters as segmentation Then send it to Counter () for statistics c3 = Counter (re.split ('\ Warriors, txt)) # using most_common () to select the 10 most frequent words print (c3.most_common (10)) are all the contents of the article "how to count the occurrence frequency of elements in the sequence by Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report