程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python handwritten notes] files and exceptions, pure liver goods

編輯:Python

Preface

Today, I'd like to share my notes with you , The pure stuff is dry , It seems that you can also read words . This is what I learn python Some data sorted out , I hope for you

Useful . If you want more information, please pay attention to it …

python Exchange of learning Q Group :903971231###
# Import Counter
from collections import Counter
def readfile():
red_lists=[]
blue_lists=[]
# Open the file and get the file handle 
with open("./balls.txt", "r",encoding='utf-8') as fp:
# Start reading the file and return a list
list1=fp.readlines()
# Traverse the entire file contents 
for i in range(len(list1)):
# Replace \n The characters of are separated by spaces 
list2=str(list1[i]).replace("\n","").split(" ")
for j in range(len(list2)):
if j==6:
# Put the blue ball into blue_lists In the list 
blue_lists.append(list2[j])
else:
# Put the red ball into red_lists In the list 
red_lists.append(list2[j])
#Counter It can quickly and conveniently perform some statistical operations on some objects , Here is the statistics of the occurrence times of the data in the list , Return to one tuple
red_count=Counter(red_lists)
blue_count=Counter(blue_lists)
#most_common It 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 blue balls with the highest frequency 
print("the blue ball:",l[:6])
if __name__=="__main__":
readfile()






Last

My handwritten notes have disappeared here , You can't avoid files and exceptions , I have shared my little book , Must learn . This article is not

Remember to call me where you will !! See you in the next chapter ~~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved