gpt4 book ai didi

python - 按预定义索引拆分 python 字符串

转载 作者:太空宇宙 更新时间:2023-11-04 08:40:47 25 4
gpt4 key购买 nike

<分区>

我有一个字符串,我想在特定位置将其拆分为一个字符串列表。拆分点存储在单独的拆分列表中。例如:

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)]]

有什么好的字符串函数可以完成这项工作,或者这是最简单的方法吗?

提前致谢!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com