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 to count the two-color ball high-frequency data by Python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "Python how to count high frequency data of two-color ball". In daily operation, I believe many people have doubts about Python how to count high frequency data of two-color ball. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "Python how to count high frequency data of two-color ball"! Next, please follow the small series to learn together!

Step1: Basic data preparation (obtained by crawler), the following is all the data from the first issue of 03 years to today, up to now a total of 2549 issues, balls.txt file content is as follows:

Step2: Analyze the data characteristics and data processing method selection, directly on the code as follows:

#import Counterfrom collections import Counter

def readfile(): red_lists=[] blue_lists=[] #Open file and get file handle with open("./ balls.txt", "r",encoding='utf-8') as fp: #Start reading files and return a list list1=fp.readlines() #traverse the entire file content for i in range(len(list1)): #Replace\n characters with spaces list2=str(list1[i]).replace("\n","").split(" ") for j in range(len(list2)): if j==6: #Blue ball put in blue_lists list blue_lists.append(list2[j]) else: #Red ball put in red_lists list red_lists.append(list2[j]) #Counter can quickly and easily do some statistical operations on some objects, here is the number of times the data in the list appears statistics, return a tuple red_count=Counter(red_lists) blue_count=Counter(blue_lists) #most_common can be used to count and sort the most common elements in a list or string, and return a list k = red_count.most_common(len(red_count)) #Output the six red balls with the highest frequency print("the red ball:",k[:6]) l = blue_count.most_common(len(blue_count)) #Output the six most frequent blue balls print("the blue ball:",l[:6]) if __name__=="__main__": readfile()

Step3: The results are as follows:

Step4: Perform Result Verification:

At this point, the study of "Python how to count high frequency data of two-color ball" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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

Internet Technology

Wechat

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

12
Report