发现自己写python的空格split还挺多坎的,尤其是最后一个是空格的情形:

def split(s):
i = 0
ans = []
while i < len(s):
start = i
# find space
while i < len(s) and s[i] != ' ':
i += 1
ans.append(s[start:i])
i += 1
if s and s[-1] == " ":
ans.append("")
return ans assert split("") == []
assert split(" ") == ["", ""]
assert split(" ") == ["", "", ""]
assert split("a") == ["a"]
assert split("a b") == ["a", "b"]
assert split(" a") == ["", "a"]
assert split("a ") == ["a", ""]
assert split(" a b") == ["", "a", "b"]
assert split("a b ") == ["a", "b", ""]
assert split("ac bcd") == ["ac", "bcd"]

  

python split space的更多相关文章

  1. python split函数

    Python split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串 例 current_month = "2013-01-02" y ...

  2. python split()函数

    Python split()函数 函数原型: split([char][, num])默认用空格分割,参数char为分割字符,num为分割次数,即分割成(num+1)个字符串 1.按某一个字符分割. ...

  3. Python split()方法

    Python split()方法 描述 Python split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串 语法 split()方法语法: str.sp ...

  4. python ---split()函数讲解

    python ---split()函数讲解 split中文翻译为分裂. 在python用于分割字符串使用. split()就是将一个字符串分裂成多个字符串组成的列表. split()可以传入参数,也可 ...

  5. python split()函数使用拆分字符串 将字符串转化为列表

    函数:split()Python中有split()和os.path.split()两个函数,具体作用如下:split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list ...

  6. python split()黑魔法

    split()用法: #!/usr/bin/python str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.spli ...

  7. 前两篇转载别人的精彩文章,自己也总结一下python split的用法吧!

    前言:前两篇转载别人的精彩文章,自己也总结一下吧! 最近又开始用起py,是为什么呢? 自己要做一个文本相似度匹配程序,大致思路就是两个文档,一个是试题,一个是材料,我将试题按每题分割出来,再将每题的内 ...

  8. python split()函数的用法

    转自: https://blog.csdn.net/orangefly0214/article/details/80810449 函数:split() Python中有split()和os.path. ...

  9. python split() 用法

    字符串的split用法 说明:Python中没有字符类型的说法,只有字符串,这里所说的字符就是只包含一个字符的字符串!!!这里这样写的原因只是为了方便理解,仅此而已. 由于敢接触Python,所以不保 ...

随机推荐

  1. Controller配置汇总

    1.通过Url对应Bean <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping ...

  2. hdu 4975 A simple Gaussian elimination problem 最大流+找环

    原题链接 http://acm.hdu.edu.cn/showproblem.php?pid=4975 这是一道很裸的最大流,将每个点(i,j)看作是从Ri向Cj的一条容量为9的边,从源点除法连接每个 ...

  3. Android SDK Manager 更新时的“https://dl-ssl.google.com refused”错误

    Android SDK Manager 消除SDK更新时的“https://dl-ssl.google.com refused”错误 消除SDK更新时,有可能会出现这样的错误:Download int ...

  4. 批量修改WORD表格属性

    有时候需要对word中很多表格的属性进行修改,而word无法批量修改属性,所有这里记录一个宏 Sub TableFormatter() Dim oTbl As Table, i As Integer ...

  5. 取汉子拼音首字母的C#方法

    /// <summary> /// 获得一个字符串的汉语拼音码 /// </summary> /// <param name="strText"> ...

  6. 【转】Spring框架深入理解

    参考这篇文章: http://www.ibm.com/developerworks/cn/java/j-lo-spring-principle/ Spring内部分为Beans, Context 和 ...

  7. [转]PHP并发IO编程之路(深度长文)

    原文:https://www.imooc.com/article/8449 -------------------------------------------------------------- ...

  8. lua 暂停写法

    由于lua 不支持暂停 用其他方法变相实现 -- 暂停 hock 写法 function _M.sleep(n) if n > 0 then os.execute("ping -c & ...

  9. jquery easyui:EasyUI Treegrid 树形网格

    用jquery easyui 的 Treegrid 树形网格 进行数据展示,不过官网的API 和 demo 让我愣了好久,摸索后整理出来供大家详细参看. jquery easyui 官网:http:/ ...

  10. Arcgis Engine(ae)接口详解(3):featureClass的feature编辑和删除

    //由于测试数据不完善,featureClass在此要只设null值,真实功能要设实际的值 IFeatureClass featureClass = null; //获取某个字段的索引,后面取字段值用 ...