Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to count the two-color ball high-frequency data by Python

Shulou Source: shulou.com Published: 2022-06-01 03:54:23 09月13日 Update

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!

Tags: Data statistics two-color two-color ball files learning maximum content characters more results basketball frequency help output convenience utility next code elements Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft OPPO Reno NVidia Linux Shulou Technology