zoukankan      html  css  js  c++  java
  • 第12课 习题讲解

     

    Python 编程作业 02

    请实现一个程序,实现如下需求点

    1.程序开始的时候提示用户输入学生年龄信息 格式如下:
    
    Jack Green ,   21  ;  Mike Mos, 9;
    
    我们假设 用户输入 上面的信息,必定会遵守下面的规则:
      学生信息之间用分号隔开(分号前后可能有不定数量的空格),
      每个学生信息里的 姓名和 年龄之间用 逗号隔开(逗号前后可能有不定数量的空格) 
    
    2. 程序随后将输入的学生信息分行显示,格式如下
    Jack Green :   21;
    Mike Mos   :   09;
    学生的姓名要求左对齐,宽度为20, 年龄信息右对齐,宽度为2位,不足前面补零

    #--------------------------------------------------------------------------------------------------------------------------------#

    需求分析:

    1、输入---input('')、返回的类型是---str

    2、要求的输入格式:Jack Green, 21;  Mike Mos,9;

    1)可以输入多个学生信息,用;隔开

    2)单个学生的名字和年龄用逗号(,)隔开

    3)逗号和分号的数量是一致的

    4)空格的数量是不固定的

    3、注意事项:如果用户不按格式输入:1、提示,格式错误;2、可以再次输入

    4、输出格式:

    Jack Green  :   21;

    Mike Mos    :    09;

    思路分解:

    1、提示用户输入:input()

    2、对输入的内容进行过滤(数据处理),做出相应的提示!

    3、获取name  age;

    while True:
        info = input('请输入学生的信息:名字, 年龄;')
        if ',' and ';' in info:
            if info.count(',') == info.count(';'):
                temp = info.split(';')
                for one in temp:
                    if one != '' and  ',' in one:
                        name ,age = one.split(',')
                        name = name.strip() # 去空格
                        age = int(age.strip())  # 去空格  --年龄转换为int型
                        print('{:<20}:{:>2}'.format(name, age))
                break
            else:
                print('您输入的格式不对:,与;的数量不一致!')
        else:
            print('您输入的格式有误!')
    
    # 执行结果
    请输入学生的信息:名字, 年龄;tom, 21;
    tom                 :21
    
    
    
    伪代码
    while True:   先定义个死循环,让用户能多次输入
        info = input('请按照格式输入你的姓名和年龄: 姓名, 年龄;')   按照格式在控制台输入姓名和年龄
            if ',' and ';' in info:  判断输入内容是否合法(是否包含','';'if info.count(',') == info.count(';')   进一步判断输入是否合法,逗号和分号是否成对出现,不然就不合法
                    temp = info.split(';')   先按照分号进行初步分割
                    for one in temp:     取列表里面的元素
                        if one != '' and ',' in one:  条件判断,如果列表里无空值并且有逗号,根据前面的条件,逗号和分号是成对出现,所以满足one里面同时包含逗号和分号(为什么用逗号,因为前面分号已经切割过了)
                            name, age = one.split(',')  # 使用逗号分隔
                            name = one.strip()  # 去掉元素两边的空格
                            age = int(one.strip())   # 去掉元素两边的空格,并且把年龄转换为int型
                            print('{:<20}:{:>02}'.format(name, age))  打印出结果
                    break   如果for循环执行完毕则跳出循环     
                else:
                    print('你输入的内容不合法--,和;的数量不一致')     如果逗号和分号的数量不一致,则报错    
            else:
                print('你输入的内容不合法,请重新输入!')           如果逗号和分号没有同时出现,则报错输入不合法
                          

    请再实现一个程序,实现如下需求点

    1.下面的log变量记录了云服务器上 当天上传的文件信息
    其中第一列是文件名,第二列是文件大小
    
    请编写一个程序,统计出不同类型的 文件的大小总和
    比如:
    jpeg  9988999
    json   324324
    png   2423233
    ----------------------------------
    
    
    log = '''
    f20180111014341/i_51a7hC3W.jpeg	169472	FrITJxleSP7wUD-MWw-phL_KP6Eu	15156063244230469	image/jpeg	0	
    f20180111014341/j_R0Hpl4EG.json	1036	ForGzwzV3e-uR3_UzvppJs1VgfQG	15156064773253144	application/json	0	
    f20180111020739/i_0TDKs0rD.jpeg	169472	FrITJxleSP7wUD-MWw-phL_KP6Eu	15156076847077556	image/jpeg	0	
    f20180111020739/j_JFO6xiir.json	1040	FmUhTchdLOd7LBoE8OXzPLDKcW60	15156077904192983	application/json	0	
    f20180111090619/i_1BwNksbL.jpg	49634	FtXBGmipcDha-67WQgGQR5shEBu2	15156329458714950	image/jpeg	0	
    f20180111090619/i_3BKlsRaZ.jpg	30152	FoWfMSuqz4TEQl5FT-FY5wqu5NGf	15156330575626044	image/jpeg	0	
    f20180111090619/i_5XboXSKh.jpg	40238	Fl84WaBWThHovIBsQaNFoIaPZcWh	15156329453409855	image/jpeg	0	
    f20180111090619/i_6DiYSBKp.jpg	74017	FrYG3icChRmFGnWQK6rYxa88KuQI	15156329461803290	image/jpeg	0	
    f20180111090619/i_76zaF2IM.jpg	38437	Fui8g5OrJh0GQqZzT9wtepfq99lJ	15156334738356648	image/jpeg	0	
    f20180111090619/i_B6TFYjks.jpg	37953	FleWqlK2W1ZmEgAatAEcm1gpR0kC	15156329464034474	image/jpeg	0	
    f20180111090619/i_N9eITqj3.jpg	38437	Fui8g5OrJh0GQqZzT9wtepfq99lJ	15156330419595764	image/jpeg	0	
    f20180111090619/i_QTSNWmA6.jpg	37953	FleWqlK2W1ZmEgAatAEcm1gpR0kC	15156333104224056	image/jpeg	0	
    f20180111090619/i_XdHcAfh1.jpg	56479	FjLQIQ3GxSEHDfu6tRcMylK1MZ05	15156334227270309	image/jpeg	0	
    f20180111090619/i_Xyy723MU.jpg	50076	FsfZpQzqu084RUw5NPYW9-Yfam_R	15156334229987458	image/jpeg	0	
    f20180111090619/i_d8Go0EOv.jpg	30152	FoWfMSuqz4TEQl5FT-FY5wqu5NGf	15156334736228515	image/jpeg	0	
    f20180111090619/i_diuHmX53.jpg	40591	FuTx1pw4idbKnV5MSvNGxCA5L470	15156333878320713	image/jpeg	0	
    f20180111090619/i_qQKzheSH.jpg	55858	Fj0A3i8V7fzzOiPQFL79ao15hkN9	15156329456666591	image/jpeg	0	
    f20180111090619/i_rHL5SYk8.jpg	40238	Fl84WaBWThHovIBsQaNFoIaPZcWh	15156336509742181	image/jpeg	0	
    f20180111090619/i_xZmQxUbz.jpg	40238	Fl84WaBWThHovIBsQaNFoIaPZcWh	15156333240603466	image/jpeg	0	
    f20180111090619/i_zBDNgXDv.jpeg	73616	FlgNwq8lypgsxrWs_ksrS_x47SQV	15156334232887875	image/jpeg	0	
    f20180111090619/j_4mxbEiVh.json	2990	Fpq-3yl3Yr1CadNrJVSDnpeRhQtT	15156331445226898	application/json	0	
    f20180111090619/j_i1K74768.json	3042	Fl5PpDw1TsZXMuhoq1RUrOeGZ6br	15156335067090003	application/json	0	
    f20180111095839/i_Q7KMKeda.png	518522	Fl-yB1_ruL2uxZN9k7DjB62h9dYH	15156359599713253	image/png	0	
    f20180111095839/j_5DpqHolV.json	184	FoYvi7cmSrzuVjUgCRzW5kU95SVo	15156359719719064	application/json	0	
    f20180111100442/i_No8kToIV.jpg	48975	Fu1cw3f--5Vpz9kLGeJfvljhCtyZ	15156364349642377	image/jpeg	0	
    f20180111100442/i_P1bkvSeg.jpg	68200	FvYe8vi46TjUKhEy_UwDqLhO6ZsW	15156363800690634	image/jpeg	0	
    f20180111100442/i_T1AulKcD.jpg	52641	Fj2YzvdC1n_1sF93ZZgrhF3OzOeY	15156364021186365	image/jpeg	0	
    f20180111100442/i_X8d8BN07.jpg	50770	FivwidMiHbogw77lqgkIKrgmF3eA	15156363969737156	image/jpeg	0	
    f20180111100442/i_g0wtOsCX.jpg	76656	Fmtixx0mP9CAUTNosjLuYQHL6k0P	15156363448222155	image/jpeg	0	
    f20180111100442/i_h5OT9324.jpg	72672	FvbIqPLTh2cQHTIBv2akUfahZa_Z	15156364401354652	image/jpeg	0	
    f20180111100442/i_he8iLYI6.jpg	49399	FjeJvwjwhU-hKZsq66UoBg9_tEJs	15156363907932480	image/jpeg	0	
    f20180111100442/i_kg29t7Pp.jpg	76293	FuYj__sSeEN7AsXMbxO24Z8Suh8d	15156364156384686	image/jpeg	0	
    f20180111100442/i_oz1YoBI1.jpg	75620	FkY3xsUMwOI01zgoH1iXXgiQeq6I	15156364089112904	image/jpeg	0	
    f20180111100442/i_xrOT98on.jpg	50021	Fql7ookM1Rc6V7VairKAfnKe-o9w	15156363856357316	image/jpeg	0	
    f20180111135114/i_Zqt8Tmoe.png	161629	FlELw59_mV3VqDBLyu1BKN4fIWnx	15156500155209863	image/png	0	
    f20180111135114/j_uhHoMXKq.json	159	FrypljwAr2LgoLAePBNTUYTUAgDt	15156500200488238	application/json	0	
    f20180111142119/i_s83iZ2GR.png	92278	Fns8tdh3JCkRmfE_COYEu4o8w03E	15156517082371259	image/png	0	
    f20180111142119/j_0g45JRth.json	159	Fq1rFwdRguYRXrp61nGZ5TsUG1V-	15156517143375596	application/json	0	
    f20180111144306/i_yE5TC84E.png	139230	Fjf61ymabEnEvnr5ZMHFjXGCrYlP	15156530038824150	image/png	0	
    f20180111144306/j_OF4WVtSH.json	159	FqwkKcxfo8jd0jFUyuH4X2CrnE9q	15156530083419530	application/json	0	
    f20180111150230/i_KtnER4g3.png	120044	FuwOWdrqzcr2-UScem-LzEMgMezs	15156541734892258	image/png	0	
    f20180111150230/j_xMSUEejY.json	158	FjJr_4deMqFphGaptm-2Pa6wwRP2	15156541771989216	application/json	0	
    f20180111151741/i_JuSWztB3.jpg	92506	FrIjRevHSi6xv4-NQa2wrHu5a1zQ	15156550875370965	image/jpeg	0	
    f20180111153550/i_9wWzVenl.gif	769872	FvslKY9JUaCQm-lu02E34tvAP_oG	15156561674621628	image/gif	0	
    '''

    需求分析:

    1、处理一个log

    2、把相同格式的文件,统计字节大小

    3、要求的格式:

    jpeg  9988999
    json   324324
    png   2423233

    思路分析

    1、获取每一行的日志信息的   文件 + 大小

    1)可以使用print查看是否存在换行---

    2、对相同格式文件进行累加

    1)size获取的时候是str类型,累加要用int类型

    2)创建一个函数,做累加。

    print(log)  # 查看日志的输出格式。三引号里的内容都有换行
    
    # 输出结果
    
    f20180111014341/i_51a7hC3W.jpeg    169472    FrITJxleSP7wUD-MWw-phL_KP6Eu    15156063244230469    image/jpeg    0    
    f20180111014341/j_R0Hpl4EG.json    1036    ForGzwzV3e-uR3_UzvppJs1VgfQG    15156064773253144    application/json    0    
    f20180111020739/i_0TDKs0rD.jpeg    169472    FrITJxleSP7wUD-MWw-phL_KP6Eu    15156076847077556    image/jpeg    0    
    f20180111020739/j_JFO6xiir.json    1040    FmUhTchdLOd7LBoE8OXzPLDKcW60    15156077904192983    application/json    0    
    f20180111090619/i_1BwNksbL.jpg    49634    FtXBGmipcDha-67WQgGQR5shEBu2    15156329458714950    image/jpeg    0    
    f20180111090619/i_3BKlsRaZ.jpg    30152    FoWfMSuqz4TEQl5FT-FY5wqu5NGf    15156330575626044    image/jpeg    0    
    f20180111090619/i_5XboXSKh.jpg    40238    Fl84WaBWThHovIBsQaNFoIaPZcWh    15156329453409855    image/jpeg    0    
    f20180111090619/i_6DiYSBKp.jpg    74017    FrYG3icChRmFGnWQK6rYxa88KuQI    15156329461803290    image/jpeg    0    
    f20180111090619/i_76zaF2IM.jpg    38437    Fui8g5OrJh0GQqZzT9wtepfq99lJ    15156334738356648    image/jpeg    0    
    f20180111090619/i_B6TFYjks.jpg    37953    FleWqlK2W1ZmEgAatAEcm1gpR0kC    15156329464034474    image/jpeg    0    
    f20180111090619/i_N9eITqj3.jpg    38437    Fui8g5OrJh0GQqZzT9wtepfq99lJ    15156330419595764    image/jpeg    0    
    f20180111090619/i_QTSNWmA6.jpg    37953    FleWqlK2W1ZmEgAatAEcm1gpR0kC    15156333104224056    image/jpeg    0    
    f20180111090619/i_XdHcAfh1.jpg    56479    FjLQIQ3GxSEHDfu6tRcMylK1MZ05    15156334227270309    image/jpeg    0    
    f20180111090619/i_Xyy723MU.jpg    50076    FsfZpQzqu084RUw5NPYW9-Yfam_R    15156334229987458    image/jpeg    0    
    f20180111090619/i_d8Go0EOv.jpg    30152    FoWfMSuqz4TEQl5FT-FY5wqu5NGf    15156334736228515    image/jpeg    0    
    f20180111090619/i_diuHmX53.jpg    40591    FuTx1pw4idbKnV5MSvNGxCA5L470    15156333878320713    image/jpeg    0    
    f20180111090619/i_qQKzheSH.jpg    55858    Fj0A3i8V7fzzOiPQFL79ao15hkN9    15156329456666591    image/jpeg    0    
    f20180111090619/i_rHL5SYk8.jpg    40238    Fl84WaBWThHovIBsQaNFoIaPZcWh    15156336509742181    image/jpeg    0    
    f20180111090619/i_xZmQxUbz.jpg    40238    Fl84WaBWThHovIBsQaNFoIaPZcWh    15156333240603466    image/jpeg    0    
    f20180111090619/i_zBDNgXDv.jpeg    73616    FlgNwq8lypgsxrWs_ksrS_x47SQV    15156334232887875    image/jpeg    0    
    f20180111090619/j_4mxbEiVh.json    2990    Fpq-3yl3Yr1CadNrJVSDnpeRhQtT    15156331445226898    application/json    0    
    f20180111090619/j_i1K74768.json    3042    Fl5PpDw1TsZXMuhoq1RUrOeGZ6br    15156335067090003    application/json    0    
    f20180111095839/i_Q7KMKeda.png    518522    Fl-yB1_ruL2uxZN9k7DjB62h9dYH    15156359599713253    image/png    0    
    f20180111095839/j_5DpqHolV.json    184    FoYvi7cmSrzuVjUgCRzW5kU95SVo    15156359719719064    application/json    0    
    f20180111100442/i_No8kToIV.jpg    48975    Fu1cw3f--5Vpz9kLGeJfvljhCtyZ    15156364349642377    image/jpeg    0    
    f20180111100442/i_P1bkvSeg.jpg    68200    FvYe8vi46TjUKhEy_UwDqLhO6ZsW    15156363800690634    image/jpeg    0    
    f20180111100442/i_T1AulKcD.jpg    52641    Fj2YzvdC1n_1sF93ZZgrhF3OzOeY    15156364021186365    image/jpeg    0    
    f20180111100442/i_X8d8BN07.jpg    50770    FivwidMiHbogw77lqgkIKrgmF3eA    15156363969737156    image/jpeg    0    
    f20180111100442/i_g0wtOsCX.jpg    76656    Fmtixx0mP9CAUTNosjLuYQHL6k0P    15156363448222155    image/jpeg    0    
    f20180111100442/i_h5OT9324.jpg    72672    FvbIqPLTh2cQHTIBv2akUfahZa_Z    15156364401354652    image/jpeg    0    
    f20180111100442/i_he8iLYI6.jpg    49399    FjeJvwjwhU-hKZsq66UoBg9_tEJs    15156363907932480    image/jpeg    0    
    f20180111100442/i_kg29t7Pp.jpg    76293    FuYj__sSeEN7AsXMbxO24Z8Suh8d    15156364156384686    image/jpeg    0    
    f20180111100442/i_oz1YoBI1.jpg    75620    FkY3xsUMwOI01zgoH1iXXgiQeq6I    15156364089112904    image/jpeg    0    
    f20180111100442/i_xrOT98on.jpg    50021    Fql7ookM1Rc6V7VairKAfnKe-o9w    15156363856357316    image/jpeg    0    
    f20180111135114/i_Zqt8Tmoe.png    161629    FlELw59_mV3VqDBLyu1BKN4fIWnx    15156500155209863    image/png    0    
    f20180111135114/j_uhHoMXKq.json    159    FrypljwAr2LgoLAePBNTUYTUAgDt    15156500200488238    application/json    0    
    f20180111142119/i_s83iZ2GR.png    92278    Fns8tdh3JCkRmfE_COYEu4o8w03E    15156517082371259    image/png    0    
    f20180111142119/j_0g45JRth.json    159    Fq1rFwdRguYRXrp61nGZ5TsUG1V-    15156517143375596    application/json    0    
    f20180111144306/i_yE5TC84E.png    139230    Fjf61ymabEnEvnr5ZMHFjXGCrYlP    15156530038824150    image/png    0    
    f20180111144306/j_OF4WVtSH.json    159    FqwkKcxfo8jd0jFUyuH4X2CrnE9q    15156530083419530    application/json    0    
    f20180111150230/i_KtnER4g3.png    120044    FuwOWdrqzcr2-UScem-LzEMgMezs    15156541734892258    image/png    0    
    f20180111150230/j_xMSUEejY.json    158    FjJr_4deMqFphGaptm-2Pa6wwRP2    15156541771989216    application/json    0    
    f20180111151741/i_JuSWztB3.jpg    92506    FrIjRevHSi6xv4-NQa2wrHu5a1zQ    15156550875370965    image/jpeg    0    
    f20180111153550/i_9wWzVenl.gif    769872    FvslKY9JUaCQm-lu02E34tvAP_oG    15156561674621628    image/gif    0    

    三引号里面的内容,用 来切分,首尾都是空字符串

    print(log.split('
    '))  # 使用
    来切分,发现首尾都是空字符串
    
    
    #输出结果
    ['', 'f20180111014341/i_51a7hC3W.jpeg	169472	FrITJxleSP7wUD-MWw-phL_KP6Eu	15156063244230469	image/jpeg	0	', 'f20180111014341/j_R0Hpl4EG.json	1036	ForGzwzV3e-uR3_UzvppJs1VgfQG	15156064773253144	application/json	0	', 'f20180111020739/i_0TDKs0rD.jpeg	169472	FrITJxleSP7wUD-MWw-phL_KP6Eu	15156076847077556	image/jpeg	0	', 'f20180111020739/j_JFO6xiir.json	1040	FmUhTchdLOd7LBoE8OXzPLDKcW60	15156077904192983	application/json	0	', 'f20180111090619/i_1BwNksbL.jpg	49634	FtXBGmipcDha-67WQgGQR5shEBu2	15156329458714950	image/jpeg	0	', 'f20180111090619/i_3BKlsRaZ.jpg	30152	FoWfMSuqz4TEQl5FT-FY5wqu5NGf	15156330575626044	image/jpeg	0	', 'f20180111090619/i_5XboXSKh.jpg	40238	Fl84WaBWThHovIBsQaNFoIaPZcWh	15156329453409855	image/jpeg	0	', 'f20180111090619/i_6DiYSBKp.jpg	74017	FrYG3icChRmFGnWQK6rYxa88KuQI	15156329461803290	image/jpeg	0	', 'f20180111090619/i_76zaF2IM.jpg	38437	Fui8g5OrJh0GQqZzT9wtepfq99lJ	15156334738356648	image/jpeg	0	', 'f20180111090619/i_B6TFYjks.jpg	37953	FleWqlK2W1ZmEgAatAEcm1gpR0kC	15156329464034474	image/jpeg	0	', 'f20180111090619/i_N9eITqj3.jpg	38437	Fui8g5OrJh0GQqZzT9wtepfq99lJ	15156330419595764	image/jpeg	0	', 'f20180111090619/i_QTSNWmA6.jpg	37953	FleWqlK2W1ZmEgAatAEcm1gpR0kC	15156333104224056	image/jpeg	0	', 'f20180111090619/i_XdHcAfh1.jpg	56479	FjLQIQ3GxSEHDfu6tRcMylK1MZ05	15156334227270309	image/jpeg	0	', 'f20180111090619/i_Xyy723MU.jpg	50076	FsfZpQzqu084RUw5NPYW9-Yfam_R	15156334229987458	image/jpeg	0	', 'f20180111090619/i_d8Go0EOv.jpg	30152	FoWfMSuqz4TEQl5FT-FY5wqu5NGf	15156334736228515	image/jpeg	0	', 'f20180111090619/i_diuHmX53.jpg	40591	FuTx1pw4idbKnV5MSvNGxCA5L470	15156333878320713	image/jpeg	0	', 'f20180111090619/i_qQKzheSH.jpg	55858	Fj0A3i8V7fzzOiPQFL79ao15hkN9	15156329456666591	image/jpeg	0	', 'f20180111090619/i_rHL5SYk8.jpg	40238	Fl84WaBWThHovIBsQaNFoIaPZcWh	15156336509742181	image/jpeg	0	', 'f20180111090619/i_xZmQxUbz.jpg	40238	Fl84WaBWThHovIBsQaNFoIaPZcWh	15156333240603466	image/jpeg	0	', 'f20180111090619/i_zBDNgXDv.jpeg	73616	FlgNwq8lypgsxrWs_ksrS_x47SQV	15156334232887875	image/jpeg	0	', 'f20180111090619/j_4mxbEiVh.json	2990	Fpq-3yl3Yr1CadNrJVSDnpeRhQtT	15156331445226898	application/json	0	', 'f20180111090619/j_i1K74768.json	3042	Fl5PpDw1TsZXMuhoq1RUrOeGZ6br	15156335067090003	application/json	0	', 'f20180111095839/i_Q7KMKeda.png	518522	Fl-yB1_ruL2uxZN9k7DjB62h9dYH	15156359599713253	image/png	0	', 'f20180111095839/j_5DpqHolV.json	184	FoYvi7cmSrzuVjUgCRzW5kU95SVo	15156359719719064	application/json	0	', 'f20180111100442/i_No8kToIV.jpg	48975	Fu1cw3f--5Vpz9kLGeJfvljhCtyZ	15156364349642377	image/jpeg	0	', 'f20180111100442/i_P1bkvSeg.jpg	68200	FvYe8vi46TjUKhEy_UwDqLhO6ZsW	15156363800690634	image/jpeg	0	', 'f20180111100442/i_T1AulKcD.jpg	52641	Fj2YzvdC1n_1sF93ZZgrhF3OzOeY	15156364021186365	image/jpeg	0	', 'f20180111100442/i_X8d8BN07.jpg	50770	FivwidMiHbogw77lqgkIKrgmF3eA	15156363969737156	image/jpeg	0	', 'f20180111100442/i_g0wtOsCX.jpg	76656	Fmtixx0mP9CAUTNosjLuYQHL6k0P	15156363448222155	image/jpeg	0	', 'f20180111100442/i_h5OT9324.jpg	72672	FvbIqPLTh2cQHTIBv2akUfahZa_Z	15156364401354652	image/jpeg	0	', 'f20180111100442/i_he8iLYI6.jpg	49399	FjeJvwjwhU-hKZsq66UoBg9_tEJs	15156363907932480	image/jpeg	0	', 'f20180111100442/i_kg29t7Pp.jpg	76293	FuYj__sSeEN7AsXMbxO24Z8Suh8d	15156364156384686	image/jpeg	0	', 'f20180111100442/i_oz1YoBI1.jpg	75620	FkY3xsUMwOI01zgoH1iXXgiQeq6I	15156364089112904	image/jpeg	0	', 'f20180111100442/i_xrOT98on.jpg	50021	Fql7ookM1Rc6V7VairKAfnKe-o9w	15156363856357316	image/jpeg	0	', 'f20180111135114/i_Zqt8Tmoe.png	161629	FlELw59_mV3VqDBLyu1BKN4fIWnx	15156500155209863	image/png	0	', 'f20180111135114/j_uhHoMXKq.json	159	FrypljwAr2LgoLAePBNTUYTUAgDt	15156500200488238	application/json	0	', 'f20180111142119/i_s83iZ2GR.png	92278	Fns8tdh3JCkRmfE_COYEu4o8w03E	15156517082371259	image/png	0	', 'f20180111142119/j_0g45JRth.json	159	Fq1rFwdRguYRXrp61nGZ5TsUG1V-	15156517143375596	application/json	0	', 'f20180111144306/i_yE5TC84E.png	139230	Fjf61ymabEnEvnr5ZMHFjXGCrYlP	15156530038824150	image/png	0	', 'f20180111144306/j_OF4WVtSH.json	159	FqwkKcxfo8jd0jFUyuH4X2CrnE9q	15156530083419530	application/json	0	', 'f20180111150230/i_KtnER4g3.png	120044	FuwOWdrqzcr2-UScem-LzEMgMezs	15156541734892258	image/png	0	', 'f20180111150230/j_xMSUEejY.json	158	FjJr_4deMqFphGaptm-2Pa6wwRP2	15156541771989216	application/json	0	', 'f20180111151741/i_JuSWztB3.jpg	92506	FrIjRevHSi6xv4-NQa2wrHu5a1zQ	15156550875370965	image/jpeg	0	', 'f20180111153550/i_9wWzVenl.gif	769872	FvslKY9JUaCQm-lu02E34tvAP_oG	15156561674621628	image/gif	0	', '']

    代码如下

    outList = []
    def sizeAdd(fileType, fileSize): # 形参
        for one in outList:
            if fileType == one[0]:  # 说明有这个格式的文件
                one[1] += fileSize
                return
        outList.append([fileType,fileSize])
    
    
    
    
    for line in log.split('
    '):
        if line != '':
            temp = line.split('	')
            fileName = temp[0].strip().split('.')[1].strip()
            size = int(temp[1].strip()) # 文件大小
            sizeAdd(fileName, size)
    
    print(outList)
    
    
    # 执行结果
    
    [['jpeg', 412560], ['json', 8927], ['jpg', 1374206], ['png', 1031703], ['gif', 769872]]

    伪代码

    outList = []  # 伪代码     结果存储为这样的格式:outList = [[gif, 20000], [png, 10000]]
    def sizeAdd(fileType, fileSize): # 形参
        for one in outList:
            if fileType == one[0]:  # 如果列表中存在此文件格式
                one[1] += fileSize  # 则在文件大小在原来的基础上累加
                return   # 结束函数,为什么不用break。因为break是跳出本次循环;而return是结束函数执行
        outList.append([fileType, fileSize])    # outList,append()放在开头位置,意思是for循环里面如果成立,则不执行append(), 反之则执行
    
    
    # print(log.split('
    '))
    for line in log.split('
    '):   # 按照换行符来切割,得到整个每句内容
        if line != '':             # 假设条件,子句内容不为空
            temp = line.split('	') # 定义temp,  按照制表符'	'来切割
            fileName = temp[0].split('.')[1].strip()        # 定义文件名 fileName,按照.来切割,去掉两边空格
            size = int(temp[1].strip()) # 文件大小,去掉空格
            #print(fileName, size)

    Python 作业 03

    请定义一个函数 mySort,参数为一个列表,参数列表中的元素都是整数.
    mySort 函数需要将参数列表中的元素按从小到大排序,最终返回一个新的list。
    
    请按下面算法的思路实现函数:
    
    1. 创建一个新的列表newList
    2. 先找出所有元素中最小的,append在newList里面
    3. 再找出剩余的所有元素中最小的,append在newList里面
    4. 依次类推,直到所有的元素都放到newList里面  

    需求分析:

    1、mySort  函数需要将列表中的元素按照从小到大排序,最终返回一个新的list。

    1)def

    2)return

    newList = []  # 存结果
    def mySort(inList):
        while len(inList) > 0:
            minData = inList[0]   # 假设法
            minIdx = 0
            indx = 0
            for one in inList:
               if minData > one:
                   minData = one
                   minIdx = indx
    
               indx += 1
            inList.pop(minIdx)  # pop()函数,括号里面填的是元素的下标
            newList.append(minData)
    
        return newList
    
    print(mySort([16, 3, 12, 18, 5, 19]))
    
    # 执行结果
    [3, 5, 12, 16, 18, 19]

    Python 作业 04

    现有文件1(如下,请保存到文件file1.txt中), 记录了公司员工的薪资,其内容格式如下
    
    name: Jack   ;    salary:  12000
     name :Mike ; salary:  12300
    name: Luk ;   salary:  10030
      name :Tim ;  salary:   9000
    name: John ;    salary:  12000
    name: Lisa ;    salary:   11000
    
    每个员工一行,记录了员工的姓名和薪资,
    每行记录 原始文件中并不对齐,中间有或多或少的空格
    
    现要求实现一个python程序,计算出所有员工的税后工资(薪资的90%)和扣税明细,
    以如下格式存入新的文件 file2.txt中,如下所示
    
    name: Jack   ;    salary:  12000 ;  tax: 1200 ; income:  10800
    name: Mike   ;    salary:  12300 ;  tax: 1230 ; income:  11070
    name: Luk    ;    salary:  10030 ;  tax: 1003 ; income:   9027
    name: Tim    ;    salary:   9000 ;  tax:  900 ; income:   8100
    name: John   ;    salary:  12000 ;  tax: 1200 ; income:  10800
    name: Lisa   ;    salary:  11000 ;  tax: 1100 ; income:   9900
    
    要求像上面一样的对齐
    tax 表示扣税金额和 income表示实际收入。注意扣税金额和 实际收入要取整数  

    思路分析:

    1、读取文件内容

    2、获取name,salary

    1)读取每一行数据:fo.read().splitlines()---去掉换行符

    inFileDir = 'D://software/HelloWorld/songqin/python/lesson13-14 习题讲解/file1.txt'
    outFileDir = 'D://software/HelloWorld/songqin/python/lesson13-14 习题讲解/file2.txt'
    
    with open(inFileDir) as rFile, open(outFileDir, 'w') as wFile:
        for line in rFile.read().splitlines():
            if ';' in line:
                info = line.split(';')
                if ':'in info[0] and  ':'in info[1]:  # 这里的条件之前写成了if ':' and ':' in info:---这样的话所有条件都不成立
                    name = info[0].split(':')[1].strip()
                    salary = int(info[1].split(':')[1].strip())  # 遇到的错误,这里不定义int型,下一步会报错
                    out = 'name:{:<8} ; salary:{:>8} ;   tax:{:>6} ; income:{:>8}
    '.format(name, salary, int(salary * 0.1), int(salary * 0.9))  # 这里要加上
    换行
                    wFile.write(out)
            else:
                print('this line is error!
    ')
    
    
    执行结果
    name:Jack     ; salary:   12000 ;   tax:  1200 ; income:   10800
    name:Mike     ; salary:   12800 ;   tax:  1280 ; income:   11520
    name:Luk      ; salary:   10030 ;   tax:  1003 ; income:    9027
    name:Tim      ; salary:    9000 ;   tax:   900 ; income:    8100
    name:John     ; salary:   12000 ;   tax:  1200 ; income:   10800
    name:Lisa     ; salary:   11000 ;   tax:  1100 ; income:    9900

    伪代码

    伪代码:
    1--定义两个文件名及文件路径
        inFileDir = '文件路径'
        outFileDir = '文件路径'
    2-- 使用with open 可以省略close()
            with open(inFileDir, 'r'---默认为只读,可以省略) as rFile, open(outFileDir, 'w') as wFile:
                  
    3--读取每行数据,使用splitlines()可以去掉换行符
        for line in rFile.read().splitlines():
    4--按照(; )进行切割,切割之前先进行条件判断,看line中是否有分号。如果有则执行下一步操作,否则提示错误
        if ';' in line:
            info = line.split(';')
    5--再按照(:)进行切割,切割之前先进行条件判断。 salary要转成int型        
        if ':' in info[0] and ':' in info[1]:
            name = info[0].split(':')[1].strip()
            salary = int(info[1].split(':')[1].strip())
    6--格式化,按照一定格式输出结果
        out = ‘name: {:>6};  salary: {:>8};   tax:{:>8};  income:{:>8}’.format(name, salary, int(salary * 0.1), int(salary * 0.9))
    7--写入文件                   
        wflie.write(out)
  • 相关阅读:
    Dubbo源码分析之ExtensionLoader加载过程解析
    大型网站系统与java中间件实践-阅读笔记
    多线程编程-设计模式之保护性暂挂(Guarded Suspesion)模式
    多线程编程-设计模式之不可变对象模式
    Spring技术内幕阅读笔记(一)
    mysql存储过程语法及实例
    Spring-boot官方案例分析之data-jpa
    Spring-boot官方案例分析之log4j
    Spring Boot应用的测试——Mockito
    linux系统安装redis
  • 原文地址:https://www.cnblogs.com/nick1998/p/10056478.html
Copyright © 2011-2022 走看看