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

Python statistics file letter occurrence times (dictionary form return result)

編輯:Python

preparation : Create a txt file , Put it in the directory where the source file is located . This example uses student.txt As a demonstration

 

Complete code :

# Declaration file name
fileName = "student.txt"
# Open as read-only txt file
ll = open(fileName,"r")
# Read all the data in the file
data = ll.read()
count = 0
res = {}
for ch in data:
# Judge whether the current character is a letter
if ch.isalpha():
if ch not in res:
res[ch] = 1
else :
res[ch] = res[ch] + 1
print(res)

The operation results are as follows :

Letter a appear 2 Time , Letter b appear 3 Time ......


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