20191213用Python实现replace方法
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方法的更多相关文章
- python字符串replace()方法
python字符串replace()方法 >>> help(str.replace)Help on method_descriptor:replace(...) S.repla ...
- Python string replace 方法
Python string replace 方法 方法1: >>> a='...fuck...the....world............' >>> b=a ...
- Python之replace()方法失效
1.背景 Titanic存活率预测案例: # 读取数据 df_train = pd.read_csv("./data/train.csv") df_train.head() OUT ...
- Python replace()方法
描述 Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次. 语法 replace()方法语法: st ...
- python中的replace()方法的使用
python中的replace()方法的使用 需求是这样的:需要将字符串的某些字符替换成其他字符 str.replace(old,new,max) 第一个参数是要进行更换的旧字符,第二个参数是新的子串 ...
- Python中的replace方法
replace 方法:返回根据正则表达式进行文字替换后的字符串的复制. stringObj.replace(rgExp, replaceText) 参数 stringObj必选项.要执行该替换的 St ...
- Python replace方法并不改变原字符串
直接给出结论:replace方法不会改变原字符串. temp_str = 'this is a test' print(temp_str.replace('is','IS') print(temp_s ...
- Python数据类型及其方法详解
Python数据类型及其方法详解 我们在学习编程语言的时候,都会遇到数据类型,这种看着很基础也不显眼的东西,却是很重要,本文介绍了python的数据类型,并就每种数据类型的方法作出了详细的描述,可供知 ...
- python字符串的方法
python字符串的方法 ############7个基本方法############ 1:join def join(self, ab=None, pq=None, rs=None): # real ...
随机推荐
- Dataframe的索引问题
1 两个Dataframe相加时,一定要注意索引是否对应再相加,利用这个特点有时可以先用set_index()将某些列置为索引列,再进行相加. import pandas as pd df1 = pd ...
- Unity—Compoent类
官方API->Componment 新引入成员 作用 字段 gameobject 该组件所在的游戏对象 tag 游戏对象的标签 Transform 添加在游戏对象上的transform组件 ...
- delphi assigned函数的用法
if not Assigned(Modeless) then Assigned()什么意思! assigned 是用来判断某一指针(pointer)或过程引用是否为nil(空),如果为空则返回假(fa ...
- JS刷新后回到页面顶部
window.location.href = location.href; 方法一: $(window).scrollTop(0); 方法二:$('html ,body').animate({ scr ...
- Python_ONLINE_习题集_1 递归
1.1 使用递归实现:计算某个数的阶乘 def func(x): if x == 2: return 2 else: return x*func(x-1) a = func(4) print(a) 2 ...
- ESP32 Ethernet to wifi
参考网址 https://github.com/espressif/esp-iot-solution/tree/master/examples/eth2wifi RMII PHY Wiring(RMI ...
- 秒懂Vuejs、Angular、React原理和前端发展历史
「前端程序发展的历史」 「 不学自知,不问自晓,古今行事,未之有也 」 我们都知道现在流行的框架:Vue.Js.AngularJs.ReactJs,已经逐渐应用到各个项目和实际应用中,它们都是MVVM ...
- [集合]Map的 entrySet() 详解以及用法(四种遍历map的方式)
Entry 由于Map中存放的元素均为键值对,故每一个键值对必然存在一个映射关系. Map中采用Entry内部类来表示一个映射项,映射项包含Key和Value (我们总说键值对键值对, 每一个键值对也 ...
- Scrapy 教程(四)-命令
scrapy 没有界面,需要命令行来操作. 非常简单,总共也就十四五个命令,分为全局命令和项目命令. 全局命令 在哪都能用 常用命令 scrapy startproject name 创建项目/工程 ...
- 理解PHP面向对象三大特性
一.封装性 目的:保护类里面的数据,让类更安全, protected和private只能在类中或子类访问,通过public提供有限的接口供外部访问,封装是控制访问,而不是拒绝访问 封装关键字:publ ...