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. framework7 底部弹层popup js关闭方法

    <div class="u-sd-btns"> <button>同意</button> <button class="popup ...

  2. wpf datagrid tooltip

    <DataGridTemplateColumn Header="购方名称" Width="260" HeaderStyle="{StaticRe ...

  3. 同时安装 Python2 & Python3 cmd下版本自由选择

    系统:win7 python2.7,python3.6同时安装,于是问题来了,python27与python36文件夹下的文件名都是python.exe 这样在cmd下,直接输入python,自动执行 ...

  4. 应用安全 - 软件漏洞 - sudo漏洞汇总

    sudo Potential bypass of Runas user restrictions(CVE-2019-14287 ) Date:2019.10.14 类型: sudo提权漏洞 影响版本: ...

  5. Mailx安装与使用

    1.卸载sendmail与postfix yum -y install mailx 2.安装mailx yum -y remove sendmail postfix 3.配置mail.rc vim / ...

  6. Hive-生成一个大文件(小文件合并)

    set hive.execution.engine=mr; --在 map-reduce 作业结束时合并小文件.如启用,将创建 map-only 作业以合并目标表/分区中的文件. set hive.m ...

  7. Spark Netty 通信框架解析

    1.RpcEndpoint: RPC端点 Spark针对每个节点(Client.Master.Worker)都称之为一个RpcEndpoint,且都实现RpcEndpoint接口,内部根据不同端点的需 ...

  8. Redis 内存满了怎么办……

    我们知道Redis是基于内存的key-value数据库,因为系统的内存大小有限,所以我们在使用Redis的时候可以配置Redis能使用的最大的内存大小. 1.通过配置文件配置 通过在Redis安装目录 ...

  9. Projection Pursuit Regression----读书笔记

    The central idea is to extract linear combinations of the inputs as derived features, and then model ...

  10. Hive常用数据库操作

    1.创建表的三种姿势 第一种 //员工表 create table if not exists default.emp( empno int, ename string, job string, mg ...