【发布时间】:2017-12-19 15:50:18
【问题描述】:
我有一个字符串,我想在特定位置将其拆分为字符串列表。分割点存储在单独的分割列表中。例如:
test_string = "thequickbrownfoxjumpsoverthelazydog"
split_points = [0, 3, 8, 13, 16, 21, 25, 28, 32]
...应该返回:
>>> ['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
到目前为止,我有这个作为解决方案,但它看起来非常复杂,因为任务如此简单:
split_points.append(len(test_string))
print [test_string[start_token:end_token] for start_token, end_token in [(split_points[i], split_points[i+1]) for i in xrange(len(split_points)-1)]]
有什么好的字符串函数可以完成这项工作,或者这是最简单的方法吗?
提前致谢!
【问题讨论】:
-
Python 没有
out-of-the-box在位置拆分功能,如果您要求内置函数