热线电话:13121318867

登录
2019-01-22 阅读量: 740
将python字符串拆分为分隔符但是特定的分隔符

有没有办法分割python字符串而不使用for循环,基本上将中间的字符串拆分为最接近的分隔符。

喜欢:

The cat jumped over the moon very quickly.

分隔符将是空格,结果字符串将是:

The cat jumped over

the moon very quickly.

我看到有一个count地方我可以看到有多少空格(虽然看不到如何返回它们的索引)。然后,我可以通过除以2来找到中间的一个,但是然后如何在此索引处对此分隔符进行拆分。查找已关闭,但它返回第一个索引(或使用rfind的第一个索引)而不是所有找到“”的索引。我可能在想这个。

###########################################

s = "The cat jumped over the moon very quickly"

l = s.split()

s1 = ' '.join(l[:len(l)//2])

s2 = ' '.join(l[len(l)//2 :])

print(s1)

print(s2)

0.0000
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子