发现自己写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. 2017-10-29-morning-清北模拟赛

    T1 遭遇 #include <algorithm> #include <cstdio> #include <cmath> inline void read(int ...

  2. 解决本地调用office组件成功,但是发布到IIS中出现的错误(检索COM类工厂中CLSID为{00024500-0000-0000-C000-000000000046}的组件时失败)

    在C#操作word或者Excel,我们可能会用到微软内置的COM组件,会出现很多问题. 如:在本地调试导出Excel没有问题,发布到IIS就有问题了,检测到的异常: 我们会发现在iis上运行的程序,没 ...

  3. Ubuntu 16.04下更新Atom

    在Ubuntu下Atom好像不会自动更新,但是可以通过这些方法去实现: 1.安装插件:https://atom.io/packages/up2date 2.使用apt源更新: sudo apt-get ...

  4. org.apache.catalina.connector.ClientAbortException: java.io.IOException: APR error:-32

    org.apache.catalina.connector.ClientAbortException: java.io.IOException: APR error:-32 Most likely, ...

  5. UICollectionView 使用 介绍

    1.1. Collection View 全家福: UICollectionView, UITableView, NSCollectionView n   不直接等效于NSCollectionView ...

  6. Action Bar详解(二)

    在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果. 一.添加A ...

  7. 有方向的运动js

    <!doctype html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  8. git log 查看版本演变历史

    1.查看git操作历史 $ git log  #git 查看git操作历史 $ git log  --oneline #git 简洁的查看git变更记录 $ git log -n4  --onelin ...

  9. cocos2d-x wp8 中文显示问题

    cocos2d-x项目在wp8下中文显示不了.有两个原因: 1. 本身字符串的utf8编码, 有问题. 2. 显示中文所须要的字体,缺失. 要解决第二个问题非常easy,在fonts下加入一个中文字体 ...

  10. weexapp 开发流程(一)开发环境配置

    1.创建项目 weexpack create weexapp 2.安装必要插件 npm i jwt-simple vue-resource vue-router vuex vuex-router-sy ...