Python中的multiplit

2024-10-01 00:14:50 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将一个字符串除以两个相反的值?例如,(和{}是“分隔符”,我有以下字符串:

Wouldn't it be (most) beneficial to have (at least) some idea?

我需要以下输出(作为数组)

^{pr2}$

Tags: to字符串mosthaveitsome数组be
3条回答

在这种特殊情况下,首先按空格分割,然后修剪括号,这听起来更有意义。在

out = []
for element in "Wouldn't it be (most) beneficial to have (at least) some idea?".split():
  out.append(element.strip('()'))

嗯。。。重读这个问题,你想保留一些空间,所以也许不是:)而是保持在这里不动。在

使用正则表达式,同时匹配()字符:

import re
re.split('[()]', string)

re.split()

s = "Wouldn't it be (most) beneficial to have (at least) some idea?"
l = re.split('[()]', s);

相关问题 更多 >