|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 小小小小的鱼丶 于 2018-7-15 17:16 编辑
这是timu
class Nstr(str):
def __sub__(self,other):
mylist = []
mylist_string = ""
for each in self:
mylist.append(each)
count = self.count(other) #other在count中出现了几次
print("%s出现了%d次"% (other,count))
index = self.find(other)#返回other在self第一次出现的索引
print("%s第一次出现的索引是%d"%(other,index))
for each_count in range(count):#接下来只需要在list中把子字符串去掉就行,因为list.remove()参数可以是字符串
print("这是第%d次循环开始:"% each_count)
for each in range(len(other)):
print(self[index+each])
mylist.remove(self[index+each]) #这里删的是mylist里面的元素,self没有被改变,list.remove() 函数用于移除列表中某个值的第一个匹配项
for each in mylist:
mylist_string += each
print("第%d次循环后的列表为:"%each_count,mylist_string)
mylist_string = ""
self = mylist_string
return self
为啥结果是这样。。 |
-
结果
-
结果
|