• 阶段小测试我的作业


    # !/usr/bin/env python3
    # -*- coding: utf-8 -*-
    # @Author : "小多肉"
    # @Email : 1021181701@qq.com
    """

    存在一一个名称为data.log的文件,data.log中 的内容是以逗号作为分隔符的,依次存储了一次测试的
    TestID,TestTime. ,Success(0成功; 1失败)。 文件中数据均为非负整数。请写一段程序(不限语言), 对所有成功
    (Success=0)的测试,输出:
    1)打印最大、最小TestTime;
    2)打印平均TestTime,保留小数点2位数。
    data.log的内容格式如下:
    TestID ,TestTime ,Success
    0,149,0
    1,69,0
    2,45,0
    3,18,1
    4,18,1
    """

    方法一:

    data_list=[]
    with open( '/Users/momoon/Downloads/data.log' , mode='r' , encoding= 'utf-8') as f:
        for line in f.readlines( ):
            line_list = line.split(',')
            data_list.append(line_list)
    test_time = []
    for i in data_list[1:]:
        test_time.append(int(i[1]))
    len = len(test_time)
    print(data_list)
    print(test_time)
    avg = sum(test_time)/len
    
    # 冒泡排序法
    for n in range(0,len-1):
        for j in range(n+1 ,len):
            if test_time[j] <test_time[n]:
                temp = test_time[n]
                test_time[n] = test_time[j]
                test_time[j] = temp
    print("{} ,{}".format(test_time[-1],test_time[0]))
    print("TtTestTime: %.2f" %avg)

    """
    输出结果:

    [['TestID', 'TestTime ', 'Success '], ['0', '149', '0 '], ['1 ', '69', '0 '], ['2', '45', '0 '], ['3', '18', '1 '], ['4', '18', '1']]
    [149, 69, 45, 18, 18]
    149 ,18
    TtTestTime: 59.80

    """

    方法二:

    import os
    def anaysis_data():
        test_times= []
        #打开data.og文件
        with open(os.getcwd() + "/data.log") as fs:
            for line in fs .readlines(): #按行读取
                temp = line.strip("
    ").split(",")#去掉换行符之后,再按,分割
                print("temp" ,temp)
                if temp[-1] == str(0): #筛选success字段为的TestTime
                    test_times.append(int(temp[-2]))
    
        if len(test_times)> 0:
            avg_time = sum(test_times) / len(test_times) #平均值
            max_time = max(test_times)
            min_time = min(test_times)
            print("最大的TestTime: " ,max_time," ,最小的TestTime: " ,min_time," ,平均TestTime: " ,avg_time)
    if __name__== '__main__' :
        anaysis_data()

    输出结果为:

    temp ['TestID', 'TestTime ', 'Success']
    temp ['0', '149', '0']
    temp ['1 ', '69', '0']
    temp ['2', '45', '0']
    temp ['3', '18', '1']
    temp ['4', '18', '1']
    最大的TestTime: 149 ,最小的TestTime: 45 ,平均TestTime: 87.66666666666667

  • 相关阅读:
    .NET平台下,初步认识AutoMapper
    python 二分查找算法
    01背包问题(动态规划)python实现
    NSSM安装服务
    iis .apk .ipa下载设置
    动态规划 转载
    leetcode 5 查找最长的回文子串
    [DEncrypt] MySecurity--安全加密/Base64/文件加密 (转载)
    [DEncrypt] HashEncode--哈希加密帮助类 (转载)
    [DEncrypt] Encrypt--加密/解密/MD5加密 (转载)
  • 原文地址:https://www.cnblogs.com/momoon/p/12147245.html
Copyright © 2020-2023  润新知