-
编写一个程序,交换指定字典的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}) -
编写一个程序,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'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) -
写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
例如: 'abc' -> 'Abc' '12asd' --> '12asd'# 这是一个实现capitalize函数的程序 str3 = input('请输入一个字符串:') print(str3[0].upper() + str3[1:]) -
写程序实现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) -
写程序实现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) -
写程序实现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, -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)
-
写程序实现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) -
写程序实现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)) -
用思维导图总结四大容器:列表、字典、元组、集合

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

被折叠的 条评论
为什么被折叠?



