如果我有一个字符串,比如说,“The quick brown fox jumps over the lazy dog”
,并且有一个列表[1, 8, 14, 18, 27]
表示在哪里截断字符串。
我希望得到的是一个包含部分剪切字符串的列表。对于这个例子,输出应该是:
['T', 'he quic', 'k brow', 'n fo', 'x jumps o', 'ver the lazy dog']
我的直觉和天真的方法是简单地编写一个 for 循环,记住之前的索引,对字符串进行切片并将切片附加到输出。
_str="The quick brown fox jumps over the lazy dog"
cut=[1, 8, 14, 18, 27]
prev=0
out=[]
for i in cut:
out.append(_str[prev:i])
prev=i
out.append(_str[prev:])
有没有更好的方法?
最佳答案
这是我的做法:
>>> s = "The quick brown fox jumps over the lazy dog"
>>> l = [1, 8, 14, 18, 27]
>>> l = [0] + l + [len(s)]
>>> [s[x:y] for x,y in zip(l, l[1:])]
['T', 'he quic', 'k brow', 'n fo', 'x jumps o', 'ver the lazy dog']
一些解释:
我将 0 添加到列表的前面,将 len(s)
添加到列表的末尾,这样
>>> zip(l, l[1:])
[(0, 1), (1, 8), (8, 14), (14, 18), (18, 27), (27, 43)]
给我一个切片索引元组序列。剩下要做的就是在列表理解中解压缩这些索引并生成您想要的切片。
编辑:
如果您真的关心此操作的内存占用,因为您经常处理非常大的字符串和列表,请一直使用生成器并构建您的列表l
这样它首先包含 0 和 len(s)
。
对于 Python 2:
>>> from itertools import izip, tee
>>> s = "The quick brown fox jumps over the lazy dog"
>>> l = [0, 1, 8, 14, 18, 27, 43]
>>>
>>> def get_slices(s, l):
... it1, it2 = tee(l)
... next(it2)
... for start, end in izip(it1, it2):
... yield s[start:end]
...
>>> list(get_slices(s,l))
['T', 'he quic', 'k brow', 'n fo', 'x jumps o', 'ver the lazy dog']
对于 Python 3:
zip
执行 izip
在 Python 2 中执行的操作(参见 Python 3.3 版本)
对于使用 yield from
语法的 Python 3.3+:
>>> from itertools import tee
>>> s = "The quick brown fox jumps over the lazy dog"
>>> l = [0, 1, 8, 14, 18, 27, 43]
>>>
>>> def get_slices(s, l):
... it1, it2 = tee(l)
... next(it2)
... yield from (s[start:end] for start, end in zip(it1, it2))
...
>>> list(get_slices(s,l))
['T', 'he quic', 'k brow', 'n fo', 'x jumps o', 'ver the lazy dog']
关于python - 在多个位置切割字符串的 pythonic 方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35670229/