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

    作业:

    # 1、写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批量修改操作
    

    ANSR:

    def change_file(file_path, old_data, new_data):
        """
    
        :param file_path: 需要修改的文件的路径
        :param old_data: 原文件的内容
        :param new_data: 目标文件的内容
        :return:
        """
        import os
        if not os.path.exists(file_path):
            print("请输入正确的路径")
            return
    
        with open(file_path, mode="rb",encoding="utf-8") as f:
            data = f.read()
            if old_data not in data:
                print("要修改的内容不存在")
                return
            else:
                new_data = data.replace(old_data,new_data)
    
        with open(file_path,mode="wt",encoding="utf-8") as f:
            f.write(new_data)
    
    # 2、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数
    

    ANSR:

    def str_count(data_str):
        """
    
        :param data_str: 传入字符串数据
        :return:
        """
        if not type(data_str) == type("str"):
            print("请输入字符串类型的数据!")
            return
        count_nums = 0
        count_alpha = 0
        count_space = 0
        count_other = 0
        for i in data_str:
            if i.isdigit():
                count_nums += 1
            elif i.isalpha():
                count_alpha +=1
            elif i.isspace():
                count_space += 1
            else:
                count_other +=1
        print("在字符串‘{}’中,【数字】{}个、【字母】{}个、【空格】{}个以及 【其他】{}个".format(data_str,count_nums,count_alpha,count_space,count_other))
    
    # 3、写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。
    

    ANSR:

    def lenth_number(user_data):
        if not type(user_data) == type("str") or type(user_data) == type([]) or type(user_data) == type(()):
            print("请输入字符串or列表or元组类型的数据!")
            return
        elif len(user_data) > 5:
            print("长度大于5")
        else:
            print("长度不大于5")
    
    # 4、写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
    

    ANSR:

    def lenth_list(user_data):
        if not type(user_data) == type([]):
            print("请输入列表类型的数据!")
            return
        elif len(user_data) > 2:
            new_list = user_data[0:2]
            print(new_list)
        else:
            return
    
    # 5、写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。
    

    ANSR:

    def get_odd_number(user_data):
        if not type(user_data) == type([]) or type(user_data) == type(()):
            print("请输入列表or元组类型的数据!")
            return
    
        new_l = []
        user_data_list=[]
    
        for i in range(len(user_data)):
            user_data_list.append(i)
    
        count = 1
        tag =True
        while tag:
            if count < len(user_data):
                new_l.append(user_data_list[count])
                count += 2
            else:
                tag = False
    
        print(new_l)
    
    # 6、写函数,检查字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
    dic = {"k1": "v1v1", "k2": [11,22,33,44]}
    PS:字典中的value只能是字符串或列表
    

    ANSR:

    def dict_check(user_data):
        if not type(user_data) == type({}):
            print("请输入字典类型的数据!")
            return
    
        elif type(user_data) == type({}):
            value_list = []
            value_list = user_data.values()
            for i in value_list:
                if not type(user_data) == type([]) or type(user_data) == type("str"):
                    print("字典的value必须是字符串or列表类型的数据!")
                    return
    
        new_dic = {}
    
        for k, v in user_data.items():
            if len(v) <= 2:
                new_dic.setdefault(k, v)
            else:
                new_value = user_data[k][0:2]
                new_dic.setdefault(k, new_value)
        user_data.update(new_dic)
    
        return user_data
    
  • 相关阅读:
    C#中的WebBrowser控件的使用
    xshell5 可用注册码
    一次多数据源 配置问题记录
    org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.core.convert.support.ConversionServiceFactory.cr
    关于 <mvc:argument-resolvers> 的一次使用记录
    补码、反码、原码 ~ ^ 运算
    mysql中int、bigint、smallint 和 tinyint的区别与长度的含义【转】
    tomcat优化记录
    判读40亿数字中是否有某个数字
    LinkedList源码疑问记录
  • 原文地址:https://www.cnblogs.com/huluhuluwa/p/13150207.html
Copyright © 2011-2022 走看看