python100例编程python100例核心知识你知道多少python100例子
下载地址 https://share.weiyun.com/Fz43Qnru
资料目录 Python练习集100题 100道Python面试题 Python100经典练习题 Python经典题目100道题 Python题库(已收录100道真题) Python100例视频讲解课程 菜鸟教程Python教程100例 130道python练习题,涵盖基础内容的方方面面
举例 replace def my_replace(source, oldsub, newsub): """ 将字符串里所有的oldsub子串替换成newsub :param source: :param old: :param new: :return: """ if not source or not oldsub: return source
new_string = "" start_index = 0 index = my_find(source, oldsub, start=start_index) while index != -1: new_string += source[start_index:index] + newsub start_index = index+len(oldsub) index = my_find(source, oldsub, start=start_index)
new_string += source[start_index:] return new_string
def my_find(source, target, start=0): """ 返回字符串source中 子串target开始的位置, 从start索引开始搜索 如果可以找到多个,返回第一个 :param source: :param target: :param start: :return: """ if not source or not target: return -1
# 不合理的搜索起始位置 if start < 0 or start >= len(source): return -1
if len(target) > len(source): return -1
for index in range(start, len(source) - len(target)+1): t_index = 0 while t_index < len(target): if target[t_index] == source[t_index+index]: t_index += 1 else: break
if t_index == len(target): return index
return -1
if __name__ == '__main__': print(my_replace('this is a book', 'this', 'it')) print(my_replace('this is a this book', 'this', 'it')) print(my_replace('this is a this bookthis', 't2his', 'it'))
|