Python3:string中的split()函数
2017-09-10 21:54
513 查看
Python3:string中的split()函数
转载请注明作者和出处:http://blog.csdn.net/u011475210操作系统:WINDOWS 10
软件版本:python-3.6.2-amd64
编 者:WordZzzz
Python3string中的split函数
Python2x中split函数
Python3x中split函数
样例
Python2.x中split()函数:
老规矩,help!>>> help(str.split) Help on method_descriptor: split(...) S.split([sep [,maxsplit]]) -> list of strings Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.
可以看出,在Python2.x中,split()返回的是字符串列表。
Python3.x中split()函数:
同样,help!>>> help(str.split) Help on method_descriptor: split(...) S.split(sep=None, maxsplit=-1) -> list of strings Return a list of the words in S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.
啊哦,在Python3.x中,split()返回的也是字符串列表。
样例:
>>> str = "this is string example....wow!!!" >>> print (str.split( )) ['this', 'is', 'string', 'example....wow!!!'] >>> print (str.split('i',1)) ['th', 's is string example....wow!!!'] >>> print (str.split('w')) ['this is string example....', 'o', '!!!']
加入正则表达式:
>>> mySent='This book is the best book on Python or M.L. I have ever laid eyes upon' >>> mySent.split() #不带参数切分 ['This', 'book', 'is', 'the', 'best', 'book', 'on', 'Python', 'or', 'M.L.', 'I', 'have', 'ever', 'laid', 'eyes', 'upon'] >>> import re >>> regEx = re.compile('\\W') #正则表达式,定义分隔符是除单词、数字外的任意字符串 >>> listOfTokens = regEx.split(mySent) #根据正则表达式的规则进行切分 >>> listOfTokens ['This', 'book', 'is', 'the', 'best', 'book', 'on', 'Python', 'or', 'M', 'L', '', 'I', 'have', 'ever', 'laid', 'eyes', 'upon'] >>> [tok for tok in listOfTokens if len(tok) > 0] #去掉空格 ['This', 'book', 'is', 'the', 'best', 'book', 'on', 'Python', 'or', 'M', 'L', 'I', 'have', 'ever', 'laid', 'eyes', 'upon'] >>> [tok.lower() for tok in listOfTokens if len(tok) > 2] #全部小写,去掉长度小于3的单词 ['this', 'book', 'the', 'best', 'book', 'python', 'have', 'ever', 'laid', 'eyes', 'upon']
系列教程持续发布中,欢迎订阅、关注、收藏、评论、点赞哦~~( ̄▽ ̄~)~
完的汪(∪。∪)。。。zzz
相关文章推荐
- python之string中的join()与split()函数
- String.Split()函数
- python 中的split()函数和os.path.split()函数
- Python 字符串 String 内建函数大全(2)
- python strip() 函数和 split() 函数的详解及实例
- java String.split()函数的用法分析
- 举例详解Python中的split()函数的使用方法
- python中string模块各属性以及函数的用法介绍
- Python中的split()函数的使用方法
- Python split() 函数 拆分字符串 将字符串转化为列
- Python split()函数默认参数
- String.Split()函数 多种使用实例
- 一个小函数:GetItemValueBySplitString
- python_string常用函数用法小结
- python中strip() 函数和 split() 函数的理解
- python学习之函数replace capitalize split zip map
- Python 字符串 String 内建函数大全(1)
- C++中string的分割函数split
- python之函数用法basestring
- python中string的操作函数