【作业】2022.4.22 周末作业

本文介绍了Python编程中对字符串和字典的一些常见操作,包括交换字典的key和value、提取字母、首字母大写、判断是否以特定字符串结尾、检查是否全为数字、转换为大写、求序列最大值、实现replace和split功能。这些实例展示了Python处理字符串和字典的强大能力。
  1. 编写一个程序,交换指定字典的key和value。

      例如:dict1={'a':1, 'b':2, 'c':3}  -->  dict1={1:'a', 2:'b', 3:'c'}  
    
    # 这是一个交换指定字典key和value的程序
    
    dict1 = {'a': 1, 'b': 2, 'c': 3}
    print({dict1[key]: key for key in dict1})
    
  2. 编写一个程序,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串

       例如: 传入'12a&bc12d-+'   -->  'abcd'  
    
    # 这是一个提取字符串中字母并拼接的程序
    
    # 方法1
    str2 = input('请输入字符串:')
    print(''.join([x for x in str2 if 'a' <= x <= 'z' or 'A' <= x <= 'Z']))
    
    # 方法2
    str2 = input('请输入字符串:')
    new_str = ''
    for x in str2:
        if 'a' <= x <= 'z' or 'A' <= x <= 'Z':
            new_str += x
    print(new_str)
    
  3. 写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母

      例如: 'abc' -> 'Abc'   '12asd'  --> '12asd'
    
    # 这是一个实现capitalize函数的程序
    
    str3 = input('请输入一个字符串:')
        print(str3[0].upper() + str3[1:])
    
  4. 写程序实现endswith的功能,判断一个字符串是否已指定的字符串结束

       例如: 字符串1:'abc231ab' 字符串2:'ab' 函数结果为: True
            字符串1:'abc231ab' 字符串2:'ab1' 函数结果为: False
    
    # 这是一个实现endswith功能的程序
    
    str41 = 'abc231ab'
    str42 = 'ab'
    if str42 == str41[-len(str42):]:
        print(True)
    else:
        print(False)
    
  5. 写程序实现isdigit的功能,判断一个字符串是否是纯数字字符串

       例如: '1234921'  结果: True
             '23函数'   结果: False
             'a2390'    结果: False
    
    # 这是一个实现isdigit功能的程序
    
    str5 = input('请输入一个字符串:')
    for x in str5:
        if x > '9' or x < '0':
            print(False)
            break
    else:
        print(True)
    
  6. 写程序实现upper的功能,将一个字符串中所有的小写字母变成大写字母

        例如: 'abH23好rp1'   结果: 'ABH23好RP1'   
    
    # 这是一个实现upper功能的程序
    
    str6 = input('请输入一个字符串:')
    new_str = ''
    for x in str6:
        if 'a' <= x <= 'z':
            new_str += chr(ord(x) - 32)
        else:
            new_str += x
    print(new_str)
    
  7. 写程序获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值

      例如: 序列:[-7, -12, -1, -9]    结果: -1   
           序列:'abcdpzasdz'    结果: 'z'  
           序列:{'小明':90, '张三': 76, '路飞':30, '小花': 98}   结果: 98
    
# 这是一个获取指定序列中元素最大值的程序

seq_target = [-7, -12, -1, -9]

if type(seq_target) == dict:
    list_value = list(seq_target.values())
    max_value = list_value[0]
    for x in list_value[1:]:
        if x > max_value:
            max_value = x
else:
    max_value = seq_target[0]
    for x in seq_target[1:]:
        if x > max_value:
            max_value = x

print(max_value)
  1. 写程序实现replace函数的功能,将指定字符串中指定的旧字符串转换成指定的新字符串

        例如: 原字符串: 'how are you? and you?'   旧字符串: 'you'  新字符串:'me'  结果: 'how are me? and me?'
    
    # 方法1:用内置函数打败内置函数
    str8 = 'how are you? and you?'
    str8_old = 'you'
    str8_new = 'me'
    print(str8_new.join(str8.split(str8_old)))
    
    # 方法2:使用while循环
    str8 = 'how are you? and you?'
    str8_old = 'you'
    str8_new = 'me'
    result = ''
    
    n0 = len(str8) 
    n1 = len(str8_old)
    
    index8 = 0
    
    # 下标在小于n0-n1时,一边遍历一边切片找目标字符串,找到了就以新字符串的形式加到结果字符串中
    # 没找到就直接把当前字符加到结果字符串中
    while index8 <= n0 - n1:
        if str8[index8:index8 + n1] == str8_old:
            result += str8_new
            index8 += n1
        else:
            result += str8[index8]
            index8 += 1
    
    # 当剩余长度小于n1时,其实已经不可能找到目标字符串了
    # 此时如果再按照上面的循环执行,切片时会下标溢出
    # 此时只需要把剩下的部分全部加到结果字符串即可
    result += str8[index8:]
    
    print(result)
    
    # 方法3:使用for循环,如果碰到在ssss中找替换sss的要求就会出问题
    
    str8 = 'how are you? and you?'
    str8_old = 'you'
    str8_new = 'me'
    result = ''
    
    n0 = len(str8)
    n1 = len(str8_old)
    
    index8_start = 0
    
    # 思路和方法2的while差不多,区别是不再每次循环都一个字符一个字符地加
    # 而是一旦找到目标字符串,就把之前的全部加到结果字符串中
    for index8_end in range(n0 - n1 + 1):
        if str8[index8_end:index8_end + n1] == str8_old:
            result += str8[index8_start:index8_end] + str8_new
            index8_start = index8_end + n1
    
    result += str8[index8_start:]
    
    print(result)
    
  2. 写程序实现split的功能,将字符串中指定子串作为切割点对字符串进行切割

    例如:原字符串: 'how are you? and you?'   切割点: 'you'  结果: ['how are ', '? and ', '?']
    
     def split1(str_target: str, str_old: str, split_count: int):
         len_target = len(str_target)
         len_old = len(str_old)
         list_result = []
         index_start = 0
         index_end = 0
         count_now = 0
     
         while index_end < len_target and count_now < split_count:
             if str_target[index_end:index_end + len_old] == str_old:
                 list_result += [str_target[index_start:index_end]]
                 index_end += len_old
                 index_start = index_end
                 count_now += 1
             else:
                 index_end += 1
         list_result += [str_target[index_start:]]
         return list_result
     
     str1 = 'how are you? and you?'
     str2 = 'you'
     print(split1(str1, str2, 2))
    
  3. 用思维导图总结四大容器:列表、字典、元组、集合
    在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sprite.Nym

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值