zoukankan      html  css  js  c++  java
  • s5_day5作业

    # 1、写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成批量修改操作
    # def number_file(file,change_s,change):
    #     import os
    #     with open(file, 'r', encoding='utf-8')as read_f,open('z_file', 'w', encoding='utf-8')as write_f:
    #         for line in read_f:
    #             if change_s in line:
    #                 line = line.replace(change_s,change)
    #             write_f.write(line)
    #     os.remove(file)
    #     os.rename('z_file', file)
    # number_file('p.txt','123','789')
    # 2、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数
    # def func(n):
    #     num1=0
    #     num2=0
    #     num3=0
    #     num4=0
    #     for i in n:
    #         if i.isdigit():
    #             num1+=1
    #         elif i.isspace():
    #             num2+=1
    #         elif i.isalpha():
    #             num3+=1
    #         else:
    #             num4+=1
    #     print('数字,%s 空格,%s 字母,%s 其他,%s'%(num1,num2,num3,num4))
    # func(input('请输入:'))
    # 3、写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。
    # def foo(in_put):
    #     if len(in_put)>5:
    #         print(in_put,'长度大于5')
    #     else:
    #         print(in_put,'长度小于等于5')
    # foo((1,2,3,4,5,6))
    # 4、写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容。
    # def foo(in_put):
    #     for i in in_put:
    #         if i.isspace():
    #             print('有空的输入')
    #         else:
    #             print('输入成功')
    # foo('hello')
    # 5、写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
    # def foo(in_put):
    #     a=[]
    #     if len(in_put) >2:
    #         a = in_put[:2]
    #     else:
    #         print(in_put)
    #     return a
    # print(foo([1,2,3,4,5,6]))
    # 6、写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。
    # def foo(n):
    #     a=[]
    #     for i in range(len(n)):
    #         if i%2==1:
    #             a.append(n[i])
    #     return a
    # print(foo(['1','2','3','4']))
    # 7、写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
    #PS:字典中的value只能是字符串或列表
    # dic = {"k1": "v1v1", "k2": [11,22,33,44]}
    # def foo(dic):
    #     for i in dic:
    #         if len(dic[i])>2:
    #             dic[i]=dic[i][:2]
    #         else:
    #             print(dic)
    #     return dic
    # print(foo(dic))
  • 相关阅读:
    STL源码剖析之_allocate函数
    PAT 1018. Public Bike Management
    PAT 1016. Phone Bills
    PAT 1012. The Best Rank
    PAT 1014. Waiting in Line
    PAT 1026. Table Tennis
    PAT 1017. Queueing at Bank
    STL源码剖析之list的sort函数实现
    吃到鸡蛋好吃,看看是哪只母鸡下的蛋:好用的Sqlite3
    cJSON
  • 原文地址:https://www.cnblogs.com/z-x-y/p/7084731.html
Copyright © 2011-2022 走看看