python – 按第二个空格拆分字符串
发布时间:2020-12-20 13:01:27 所属栏目:Python 来源:网络整理
导读:输入: "The boy is running on the train" 预期产量: ["The boy","boy is","is running","running on","on the","the train"] 在python中实现这一目标的最简单方法是什么? 解决方法 你拆分所有空格,然后重新加入对: words = inputstr.split()secondwords
输入:
"The boy is running on the train" 预期产量: ["The boy","boy is","is running","running on","on the","the train"] 在python中实现这一目标的最简单方法是什么? 解决方法
你拆分所有空格,然后重新加入对:
words = inputstr.split() secondwords = iter(words) next(secondwords) output = [' '.join((first,second)) for first,second in zip(words,secondwords)] 演示: >>> inputstr = "The boy is running on the train" >>> words = inputstr.split() >>> secondwords = iter(words) >>> next(secondwords) # output is ignored 'The' >>> [' '.join((first,second)) for first,secondwords)] ['The boy','boy is','is running','running on','on the','the train'] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读