python split space
2024-10-21 10:29:46
发现自己写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"]
最新文章
- Lind.DDD.LindAspects方法拦截的介绍
- C++哈夫曼树编码和译码的实现
- git 教程(11)--从远程库克隆
- MySQL表中数据的迁移
- CLH锁 、MCS锁
- 如何计算oracle数据库内存
- 翻译brent ozar的sqlserver dba训练课程——第一章:建立数据库服务器清单
- Linux进程笔记
- Performing a full database disaster recovery with RMAN
- Igor In the Museum(搜搜搜151515151515******************************************************1515151515151515151515)
- 《Java4Android视频教程》学习笔记(一)
- HDU1002——大整数加法
- OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法
- 四。Hibernate 使用MAVEN工具
- Rest Framework
- 20165306 Exp2 后门原理与实践
- UVA816-Abbott&#39;s Revenge(搜索进阶)
- C# 执行bat批处理文件
- APP 渠道推广【摘自网络】
- 转载:GBDT算法梳理
热门文章
- 标准C程序设计七---07
- Nginx反向代理新篇-使用location对多个URL做反向代理
- 感受lambda之美,推荐收藏,需要时查阅
- Matlab中配置VLFeat
- hadoop-mapreduce中reducetask执行分析
- iOS --- 总结Objective-C中经常使用的宏定义(持续更新中)
- grunt 试用笔记
- flex中dispatchEvent的用法(自定义事件) .
- 新生入学V3.0颗粒归仓
- ISC DHCP: Enterprise grade solution for configuration needs