首页 > 编程语言 >Python for Everybody

Python for Everybody

时间:2024-03-31 12:11:21浏览次数:29  
标签:Everybody word name Python percent file print line

Tuples  

 Exercise 1: Revise a previous program as follows: Read and parse the “From” lines and
pull out the addresses from the line. Count the number of messages from each person
using a dictionary.
After all the data has been read, print the person with the most commits by creating a list
of (count, email) tuples from the dictionary. Then sort the list in reverse order and print
out the person who has the most commits.
Sample Line:
From [email protected] Sat Jan 5 09:14:16 2008
Enter a file name: mbox-short.txt
[email protected] 5
Enter a file name: mbox.txt
[email protected] 195

def test(file_name):
    
    try:
        file = open(file_name)
    except:
        print("file %s not found"%file_name)
        exit()
    c = dict()
    for line in file:
        line = line.rstrip()
        if line.startswith("From") and not line.startswith("From:") :
            word = line.split()[1]
            if word not in c.keys():
                c[word] = 1
            else:
                c[word] += 1
    l = []
    for key,value in list(c.items()):
        l.append((key,value))
    l.sort(reverse = True)
    print(l)
    max_guy,max = l[0]
    for x,y in l:
        if y > max:
            max_guy = x
            max = y
    print(max_guy,max)
test("mbox-short.txt")

 Exercise 2: This program counts the distribution of the hour of the day for each of the
messages. You can pull the hour from the “From” line by finding the time string and
then splitting that string into parts using the colon character. Once you have accumulated
the counts for each hour, print out the counts, one per line, sorted by hour as shown
below.
Sample Execution:
python timeofday.py
Enter a file name: mbox-short.txt
04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1
 

def test(file_name):
    try:
        file = open(file_name)
    except:
        print("file %s not found"%file_name)
        exit()
    c = dict()
    for line in file:
        line = line.rstrip()
        if line.startswith("From") and not line.startswith("From:") :
            word = line.split()[5].split(":")[0]
            if word not in c.keys():
                c[word] = 1
            else:
                c[word] += 1
    l = []
    for key,value in list(c.items()):
        l.append((key,value))
    l.sort(reverse = False)
    print(l)
    for x,y in l:print(x,y)
test("mbox-short.txt")

 Exercise 3: Write a program that reads a file and prints the letters in decreasing order
of frequency. Your program should convert all the input to lower case and only count
the letters a‑z. Your program should not count spaces, digits, punctuation, or anything
other than the letters a‑z. Find text samples from several different languages and see
how letter frequency varies between languages. Compare your results with the tables at
wikipedia.org/wiki/Letter_frequencies. 

import string

def test(file_name):
    try:
        file = open(file_name)
    except:
        print("file %s not found"%file_name)
        exit()
    c = dict()
    for line in file:
        words = line.rstrip().translate(str.maketrans('', '', string.punctuation)).lower().split()
        for word in words:
            for i in word:
                if i not in(['0','1','2','3','4','5','6','7','8','9']):
                    if i not in c:
                        c[i] = 1
                    else:
                        c[i] += 1
    # print(c)
    l = list(c.items())
    sorted_letters = sorted(c.items(), key=lambda item: item[1], reverse=True)#sort by digital value
    l.sort()#sort by letter 
    # print(l)
    count_percent = 0
    for x,y in sorted_letters:
        percent = round(y/sum(c.values())*100,2)
        Letter = x.upper()
        count_percent += percent
        print("%s: %.2f%%"%(Letter,percent))
    # print("total percent is %f%%, the bias error is %f%%"%(count_percent,(100-count_percent)))
test("mbox-short.txt")

 

标签:Everybody,word,name,Python,percent,file,print,line
From: https://www.cnblogs.com/millionyh/p/18106563

相关文章

  • 矩阵匹配【华为OD机试JAVA&Python&C++&JS题解】
    一.题目-矩阵匹配从一个NM(N<=M)的矩阵中选出N个数,任意两个数字不能在同一行或同一列,求选出来的N个数中第K大的数字的最小值是多少。输入描述:输入矩阵要求:1<=K<=N<=M<=150输入格式:NMKNM矩阵输出描述:N*M的矩阵中可以选出M!/N!种组合数组,每个组合数组中第K大的数中的......
  • 文本统计分析【华为OD机试JAVA&Python&C++&JS题解】
    一.题目-文本统计分析有一个文件,包含以一定规则写作的文本,请统计文件中包含的文本数量规则如下文本以";“分隔,最后一条可以没有”;",但空文本不能算语句,比如"COMMANDA;;"只能算一条语句.注意,无字符/空白字符/制表符都算作"空"文本文本可以跨行,比如下面,......
  • Python之Opencv进阶教程(2):统计图片灰度级别的像素数量
    1、什么是灰度像素数量在OpenCV中,可以使用**cv2.calcHist()**函数来计算图像的直方图。直方图是一种图形统计表,用于表示图像中每个灰度级别(或颜色通道)的像素数量或密度分布。以下是一个示例代码,演示了如何使用OpenCV计算和绘制图像的直方图:2、代码importcv2ascvimpor......
  • Python之Opencv进阶教程(1):图片模糊
    1、Opencv提供了多种模糊图片的方法加载原始未经模糊处理的图片importcv2ascvimg=cv.imread('../Resources/Photos/girl.jpg')cv.imshow('girl',img)1.1平均值关键代码#Averaging平均值a......
  • Python之Opencv教程(5):识别视频中的人脸
    1、识别效果2、识别代码importcv2ascvdefface_detect_demo(img):#将图片灰度gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)#加载特征数据face_detector=cv.CascadeClassifier("data//haarcascade_frontalface_alt.xml")fac......
  • Python装饰器实战:打造高效性能计时工具
    在数据分析工作中,针对百万,千万级别的数据进行分析是常有的事情,因此,分析代码性能的重要性不容忽视,能够有一个方便快速的测试函数性能的方法,对于我们快速发现性能瓶颈,及时优化,提高项目的开发效率至关重要。本文介绍如何通过Python装饰器来实现性能计时工具,帮助我们在不改变现有代码......
  • Python 潮流周刊第 44 期(摘要)+ 赠书 5 本《明解Python算法与数据结构》
    本周刊由Python猫出品,精心筛选国内外的250+信息源,为你挑选最值得分享的文章、教程、开源项目、软件工具、播客和视频、热门话题等内容。愿景:帮助所有读者精进Python技术,并增长职业和副业的收入。周刊全文:https://pythoncat.top/posts/2024-03-30-weekly特别提醒:本期赠书5......
  • 6-92 链表基本操作(Python语言描述)
    本题要求在给出的链节点类、和单链表类部分代码的情况下,编写好基本操作的各种python3代码实现,最后调用测试代码检验。函数接口定义:已实现的链节点类、和单链表类部分代码。如下:classSingleNode(object):"""单链表的结点"""def__init__(self,item):#_i......
  • 在python中通过面向对象方式,实现烤地瓜案例
    例子:烤地瓜,不同时间,反馈不同状态,并给不同状态地瓜加入不同味道烤地瓜时间0-3分钟,生的4-7分钟,半生不熟的8-12分钟,熟了12分钟以上,已烤熟,糊了用户可以按自己的意思添加调料烤地瓜类:SweetPotato时间:cooktime状态:Cookstate调料:condiments代码实现:classSweetPot......
  • 使用Python清理重复音乐文件:一个简单的解决方案
    在日常生活中,我们经常会从各种渠道获取音乐资源,例如购买、下载或者从朋友那里借来。然而,有时候我们可能会发现自己的音乐库里存在着大量的重复音乐文件,这不仅浪费了存储空间,而且在听歌的时候也会带来不便。针对这个问题,我编写了一个简单的Python程序来帮助清理重复的音乐文件。为......