函数之综合练习(敲重点)

1. 编写一个函数,交换指定字典的key和value。
def exchange_key_value(dic: dict):
    # dic = {'a': 1, 'b': 2}
    new_dic = {dic[key]: key for key in dic}
    print(new_dic)

dict1 = {'a': 1, 'b': 2}
exchange_key_value(dict1)    # {1: 'a', 2: 'b'}
2.编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
def get_alpha(string: str):
    # 方法一:
    # new_str = ''
    # for x in string:
    #     if 'a' <= x <= 'z' or 'A' <= x <= 'Z':
    #         new_str += x
    # print(new_str)

    # 方法二:
    # s1 = [x for x in string if 'a' <= x <= 'z' or 'A' <= x <= 'Z']
    # new_str = ''.join(s1)
    # print(new_str)

    new_str1 = ''.join([x for x in string if 'a' <= x <= 'z' or 'A' <= x <= 'Z'])
    print(new_str1)


str1 = '你好, Python!123, h5'
get_alpha(str1)

3.写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
def capitalize(string: str):
    first_char = string[0]
    if 'a' <= first_char <= 'z':
        new_str = first_char.upper()+string[1:]
    else:
        new_str = string
    print(new_str)


str1 = 'm123banc'
capitalize(str1)
4.写一个自己的endswith函数,判断一个字符串是否已指定的字符串结束
def endswith(string: str, end: str):
    if string[-len(end):] == end:
        print(True)
    else:
        print(False)


str1 = 'hello python'
str2 = 'python'
endswith(str1, str2)
5.写一个自己的isdigit函数,判断一个字符串是否是纯数字字符串
def isdigit(string: str):
    for x in string:
        if not '0' <= x <= '9':
            print(False)
            break
    else:
        print(True)
6.写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母
def upper(string: str):
    new_str = ''
    for x in string:
        if 'a' <= x <= 'z':
            new_str += chr(ord(x)-32)
        else:
            new_str += x
    print(new_str)
7.写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充 abc -> xxxxabc
def rjust(string: str, length: int, fill_char: str):
    length1 = len(string)
    if length1 > length:
        print(string)
    else:
        new_str = (length - length1) * fill_char + string
        print(new_str)
8.写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回-1
def index(li: list, obj):
    indexs = []
    for x in range(len(li)):
        if li[x] == obj:
            indexs.append(x)
    if indexs:
        print(indexs)
    else:
        print(-1)


index([10, 20, 30, 10, 10], 100)

9.写一个自己的len函数,统计指定序列中元素的个数
def len1(seq):
    count = 0
    for _ in seq:
        count += 1
    print(count)

10.写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
ddef in_function(seq):
    if type(seq) == dict:
        result = [i for i in seq.values()]
        num = result[0]
        for j in result:
            if num < j:
                num = j
        print(num)
    else:
        m = seq[0]
        for i in seq[1:]:
            if i > m:
                m = i
        print(m)
in_function({'小明':90, '张三': 76, '路飞':30, '小花': 98})

11.写一个函数实现自己in操作,判断指定序列中,指定的元素是否存在
def in1(seq, item):
    for x in seq:
        if x == item:
            print(True)
            break
    else:
        print(False)

12.写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串
def replace(string: str, old: str, new: str):
    # 方法一:
    # new_str = new.join(string.split(old))
    # print(new_str)
    # 方法二:
    length1 = len(string)
    length2 = len(old)
    index = 0
    new_str = ''
    while True:
        if string[index: index+length2] == old:
            new_str += new
            index += length2
        else:
            new_str += string[index]
            index += 1

        if index >= length1:
            break
    print(new_str)


replace('how are you?', ' ', '+')
replace('how are you?', 'you', 'me')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值