闲来无事写的一个python作业

作业内容:基于文末文件格式数据的统计计算。搭建好python环境,复制代码即可直接运行。

代码正文:

import os

current_script_path = os.path.realpath(__file__)
# 根据当前脚本的文件路径构建要打开的文件的绝对路径
file_name = input("<请输入文件名>")
file_path = os.path.join(os.path.dirname(current_script_path), file_name)

try:
    # 打开用户输入的文件并读取内容
    with open(file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()

        check_zid_num = {}

        man_zid = {}
        man_rbc = {}
        man_wbc = {}

        woman_zid = {}
        woman_rbc = {}
        woman_wbc = {}
        for i, line in enumerate(lines):
            if i==0:
                continue
            else:
                linesplit = line.split("\t");
                if linesplit[0] not in check_zid_num:
                    check_zid_num[linesplit[0]] = 1
                else: check_zid_num[linesplit[0]] = check_zid_num[linesplit[0]] + 1

                if linesplit[1] == "男":
                    if linesplit[0] not in man_zid:
                        man_zid[linesplit[0]] = linesplit[0]
                        man_rbc[linesplit[0]] = float(linesplit[2])
                        man_wbc[linesplit[0]] = float(linesplit[3])
                else:
                    if linesplit[0] not in woman_zid:
                        woman_zid[linesplit[0]] = linesplit[0]    
                        woman_rbc[linesplit[0]] = float(linesplit[2])
                        woman_wbc[linesplit[0]] = float(linesplit[3])

        man_rbc_avg = format(sum(man_rbc.values())/len(man_rbc),'.3f') if len(man_rbc)>0 else "NA"
        man_wbc_avg = format(sum(man_wbc.values())/len(man_wbc),'.3f') if len(man_wbc)>0 else "NA"
        woman_rbc_avg = format(sum(woman_rbc.values())/len(woman_rbc),'.3f') if len(woman_rbc)>0 else "NA"
        woman_wbc_avg = format(sum(woman_wbc.values())/len(woman_wbc),'.3f') if len(woman_wbc)>0 else "NA"
        print(f"<总人次>{len(lines)-1}<男女人数>{len(man_zid)}/{len(woman_zid)}<RBC>{man_rbc_avg}/{woman_rbc_avg}<WBC>{man_wbc_avg}/{woman_wbc_avg}")

        #统计检查次数中各个次数的人数
        check_num = {}
        check_num_person = {}
        for key in check_zid_num:
            num = check_zid_num[key];
            if num not in check_num:
                check_num[num] = 1
                check_num_person[num] = key.strip()
            else: 
                check_num[num] +=1
                check_num_person[num] = check_num_person[num] + "," + (key)

        print(f"<检查人次统计如下>")
        sorted_check_num = sorted(check_num.keys());
        for key in sorted_check_num:
            print(f"{key}次{check_num[key]}人")

        sorted_keys = sorted(check_num_person.keys(),reverse=True);
        sorted_keys_max_num = sorted_keys[0]
        for key in sorted_keys:
            checked_person = check_num_person[key];
            checked_person_split = checked_person.split(",");
            checked_person_split = sorted(checked_person_split);
            if sorted_keys_max_num == key:
                print(f"<检查{key}次的人按编号从小到大排列如下>")
                for person in checked_person_split:
                    print(person)
except FileNotFoundError:
    print(f"<文件{file_name}不存在>")

#上面使用到的txt文件格式如下

zID    Gender    RBC(10^12/L)    WBC(10^9/L)    Hb(g/L)    LY(10^9/L)
Z000001    女    3.8543    6.6906    156.135.1    3.1976
Z000002    女    4.8543    8.6906    256.135.1    4.1976
Z000003    女    3.8543    6.6906    156.135.1    3.1976
Z000003    女    4.8543    8.6906    256.135.1    4.1976
Z000002    女    3.8543    6.6906    156.135.1    3.1976
Z000003    女    4.8543    8.6906    256.135.1    4.1976
Z000002    女    3.8543    6.6906    156.135.1    3.1976
Z000003    女    4.8543    8.6906    256.135.1    4.1976
Z000002    女    3.8543    6.6906    156.135.1    3.1976
Z000006    女    4.8543    8.6906    256.135.1    4.1976
Z000008    女    3.8543    6.6906    156.135.1    3.1976
Z000009    男    4.8543    8.6906    256.135.1    4.1976

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值