Python在括号内的空格或句子上拆分字符串
我想知道是否可以拆分一个字符串,例如Python在括号内的空格或句子上拆分字符串,python,string,split,Python,String,Split,我想知道是否可以拆分一个字符串,例如 string = 'hello world [Im nick][introduction]' 放入数组中,例如 ['hello', 'world', '[Im nick][introduction]'] 它不一定是有效的,但只是一种从一个句子中分割出所有单词的方法,除非它们在括号中,整个句子都没有分割 我需要这个,因为我有一个降价文件,里面有这样的句子 - What is the weather in [San antonio, texas][locat
string = 'hello world [Im nick][introduction]'
放入数组中,例如
['hello', 'world', '[Im nick][introduction]']
它不一定是有效的,但只是一种从一个句子中分割出所有单词的方法,除非它们在括号中,整个句子都没有分割
我需要这个,因为我有一个降价文件,里面有这样的句子
- What is the weather in [San antonio, texas][location]
我需要圣安东尼奥德州是一个完整的数组内的句子,这是可能的吗?该阵列的外观如下所示:
array = ['what', 'is', 'the', 'weather', 'in', 'San antonio, texas][location]']
您可以使用带有lookback/lookahead的regex split,注意,使用filter或列表理解来过滤空条目比在re中避免要简单
import re
s = 'sss sss bbb [zss sss][zsss ss] sss sss bbb [ss sss][sss ss]'
[x for x in re.split(r"(?=\[[^\]\[]+\])* ", s)] if x]
也许这对你有用:
>>> s = 'What is the weather in [San antonio, texas][location]'
>>> i1 = s.index('[')
>>> i2 = s.index('[', i1 + 1)
>>> part_1 = s[:i1].split() # everything before the first bracket
>>> part_2 = [s[i1:i2], ] # first bracket pair
>>> part_3 = [s[i2:], ] # second bracket pair
>>> parts = part_1 + part_2 + part_3
>>> s
'What is the weather in [San antonio, texas][location]'
>>> parts
['What', 'is', 'the', 'weather', 'in', '[San antonio, texas]', '[location]']
它搜索左括号,并在按空格拆分之前将其用作引用
这假定:
- 第一个结束括号和第二个开始括号之间没有其他文字
- 在第二个结束括号之后没有任何内容
下面是一个更健壮的解决方案:
def do_split(s):
parts = []
while '[' in s:
start = s.index('[')
end = s.index(']', s.index(']')+1) + 1 # looks for second closing bracket
parts.extend(s[:start].split()) # everything before the opening bracket
parts.append(s[start:end]) # 2 pairs of brackets
s = s[end:] # remove processed part of the string
parts.extend(s.split()) # add remainder
return parts
这将产生:
>>> do_split('What is the weather in [San antonio, texas][location] on [friday][date]?')
['What', 'is', 'the', 'weather', 'in', '[San antonio, texas][location]', 'on', '[friday][date]', '?']
也许这个简短的片段可以帮助你。但是请注意,只有当您所说的一切对文件中的所有条目都成立时,这才有效
s = 'What is the weather in [San antonio, texas][location]'
s = s.split(' [')
s[1] = '[' + s[1] # add back the split character
mod = s[0] # store in a variable
mod = mod.split(' ') # split the first part on space
mod.append(s[1]) # attach back the right part
print(mod)
产出:
['What', 'is', 'the', 'weather', 'in', '[San antonio, texas][location]']
对于s='helloworld[imnick][introduction]'
['hello', 'world', '[Im nick][introduction]']
下面的代码适用于您的示例。希望有帮助:) 我相信会更好,但现在我得走了。请欣赏
string = 'hello world [Im nick][introduction]'
list = string.split(' ')
finall = []
for idx, elem in enumerate(list):
currentelem = elem
if currentelem[0] == '[' and currentelem[-1] != ']':
currentelem += list[(idx + 1) % len(list)]
finall.append(currentelem)
elif currentelem[0] != '[' and currentelem[-1] != ']':
finall.append(currentelem)
print(finall)
让我提供一个替代上述方案:
import re
string = 'hello world [Im nick][introduction]'
re.findall(r'(\[.+\]|\w+)', string)
产生:
['hello', 'world', '[Im nick][introduction]']
对于单行程序,使用功能性编程工具,如
functool
模块中的reduce
reduce( lambda x, y: x.append(y) if y and y.endswith("]") else x + y.split(), s.split(" ["))
或者,使用标准运算符,map
和sum
sum(map( lambda x: [x] if x and x.endswith("]") else x.split()), []) s.split(" ["))
在按空格拆分之前,您是否尝试过使用
.split(“[”)
的一些组合?谢谢。这确实适用于我所说的所有内容,效果非常好。一件事是,如果有N个括号对,是否有可能捕获所有括号对?例如,如果我有一句话“德克萨斯州圣安东尼奥的天气如何?”[地点]于[星期五][日期]”可以同时捕获位置和日期吗?@NickD我的解决方案将每个括号对作为它们自己的字符串放在部分中。我刚刚注意到,显然在你的问题中,有一个示例,其中一个字符串中有两对括号。请告诉你。@NickD我刚刚解决了这个问题。不适用于多个父字符串hesa组,只需尝试“hello world[Im nick][introduction]hello world[Im nick][introduction]”,而不用括号