实现Python str.split功能的小函数
2015-12-05 19:52
716 查看
s=""" tt qq xbc wzx ydc eeff mm asdfa sd s s ssss ss sss sa aa aa q qq sd d """ lis = s.split() for k in lis: print k i=0 tem = "" for t in s: if t!=" ": tem = tem + t if i+1 < len(s): if s[i+1]==" ": print tem tem="" else: print tem i=i+1
以上为以空格为分隔符,以下为以'..'为分隔符。
s="s...s.....d......r....e..." lis = s.split('..') print lis i=0 k=0 tem = "" for t in s: if t!=".": tem = tem + t j = i if i+1 < len(s): if s[i+1]==".": print tem tem="" else: print tem else: if i+1 < len(s): k=i if s[k+1]!="." and (k-j)%2==0: print k,j if s[k+1]!="." and (k-j)%2!=0: print "Cant not !" else: if (k-j+1)%2!=0: print j,k print "Can not !" i=i+1
自定义函数 my_split 实现系统自带split的函数
def my_split (src,sep): a= src.find("..") w1=src[:a] print w1 b=a i=0 while b<=len (src) and b != -1: while src[a:a+len(sep)] == sep: a=a+len(sep) b=src.find(sep,a) if b != -1: print src[a:b] else : print src [a:] a=b i=i+1 my_split ("wwwww..wwww..wwww..www",".")
def my_split (src,sep): b= src.find (sep) e= src.find (sep) if b== 0: print "\'\'" else: print src [:e] while e !=-1: b=src.find (sep,e) e=src.find(sep,b+len(sep)) if e-b!=len(sep) and e-b!=-1 : print src [b+len(sep):e] elif e==-1: print src[b:] else: print "\'\'" s="aaa...a..a.....a...addd" my_split(s,'..')
相关文章推荐
- python读取图像
- Python爬虫进阶一之爬虫框架Scrapy安装配置
- 定义 Model兼容python2.x和python3.x
- Mac 环境下下配置opencv +python
- python 字符串与16进制互转
- 通过python的obbc模块连接mysql数据库
- Climbing Stairs-python
- Python火车票代码
- Python 异常处理
- Python 文件I/O
- Python 模块
- Python函数
- Python 日期和时间
- Python 字典(Dictionary)
- Python 元组
- 【Python】Python 三种导入模块的方法和区别
- Python爬虫入门七之正则表达式
- python基础教程共60课-第7课if
- Python爬虫入门六之Cookie的使用
- python 函数式编程 高阶函数 装饰器