def myReplace(s,sub, dest, times =None):
#如果times是None,替换的次数是s.count(sub)
if times == None:
times = s.count(sub)
sub_index = []
sub_length = len(sub)
dest_length = len(dest)
s = list(s)
for i in range(len(s)):
if s[i:i+sub_length] == list(sub):
sub_index.append(i) n = 0
for index in sub_index:
if times > 0:
offset = n * (dest_length - sub_length)
index = index + offset
s[index:index+sub_length] = list(dest)
n += 1
times -= 1
return "".join(s) print(myReplace("abcc1aha",'a','xy'))
print(myReplace("abcdefgabca","a","",1))

20191213用Python实现replace方法的更多相关文章

  1. python字符串replace()方法

    python字符串replace()方法 >>> help(str.replace)Help on method_descriptor:replace(...)    S.repla ...

  2. Python string replace 方法

    Python string replace   方法 方法1: >>> a='...fuck...the....world............' >>> b=a ...

  3. Python之replace()方法失效

    1.背景 Titanic存活率预测案例: # 读取数据 df_train = pd.read_csv("./data/train.csv") df_train.head() OUT ...

  4. Python replace()方法

    描述 Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次. 语法 replace()方法语法: st ...

  5. python中的replace()方法的使用

    python中的replace()方法的使用 需求是这样的:需要将字符串的某些字符替换成其他字符 str.replace(old,new,max) 第一个参数是要进行更换的旧字符,第二个参数是新的子串 ...

  6. Python中的replace方法

    replace 方法:返回根据正则表达式进行文字替换后的字符串的复制. stringObj.replace(rgExp, replaceText) 参数 stringObj必选项.要执行该替换的 St ...

  7. Python replace方法并不改变原字符串

    直接给出结论:replace方法不会改变原字符串. temp_str = 'this is a test' print(temp_str.replace('is','IS') print(temp_s ...

  8. Python数据类型及其方法详解

    Python数据类型及其方法详解 我们在学习编程语言的时候,都会遇到数据类型,这种看着很基础也不显眼的东西,却是很重要,本文介绍了python的数据类型,并就每种数据类型的方法作出了详细的描述,可供知 ...

  9. python字符串的方法

    python字符串的方法 ############7个基本方法############ 1:join def join(self, ab=None, pq=None, rs=None): # real ...

随机推荐

  1. JDK7新特性

    二进制字面量 数字字面量可以出现下划线 switch语句可以用字符串 泛型简化 异常的多个catch合并 try..with...resource语句 import java.io.FileReade ...

  2. 彻底理解RSA算法原理

    1. 什么是RSA RSA算法是现今使用最广泛的公钥密码算法,也是号称地球上最安全的加密算法.在了解RSA算法之前,先熟悉下几个术语 根据密钥的使用方法,可以将密码分为对称密码和公钥密码 对称密码:加 ...

  3. ES 集群管理(集群规划、集群搭建、集群管理)

    一.集群规划 搭建一个集群我们需要考虑如下几个问题: 1. 我们需要多大规模的集群? 2. 集群中的节点角色如何分配? 3. 如何避免脑裂问题? 4. 索引应该设置多少个分片? 5. 分片应该设置几个 ...

  4. date picker with jquery

    <html> <input id="from_time" name="from_time"type="text" valu ...

  5. 【EWM系列】SAP EWM凭证对象表概览

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[EWM系列]SAP EWM凭证对象表概览   ...

  6. 应用安全 - 工具 | 数据库 - redis - 漏洞 - 汇总

    未授权访问 Date 类型 未授权访问导致getshell 影响范围 复现 (1)shell反弹 (2)结合SSH服务 (3)结合web服务 分析

  7. .net core 学习小结之环境配置篇

    安装IIs对 netcore 的支持 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/aspnet-core-mod ...

  8. excel导入导出(一)

    excel导入导出 依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi& ...

  9. [转帖]同事推荐的的aira2

    Windows系统安装最新版Aria2客户端及使用教程 https://www.moerats.com/archives/519/ 改天学习一下. 说明:之前都是说的在Linux VPS服务器上安装A ...

  10. Python内置函数eval

    英文文档: eval(expression, globals=None, locals=None) The arguments are a string and optional globals an ...