如何在 Python 中拆分具有多个单词分隔符的字符串?
How do I split a string with multiple word delimiters in Python?
我想要一种使用单词列表作为分隔符来拆分字符串列表的有效方法。输出是另一个字符串列表。
我在一行中尝试了多个 .split
,这不起作用,因为第一个 .split
returns 是一个列表,后面的 .split
需要一个字符串。
这是输入:
words = ["hello my name is jolloopp", "my jolloopp name is hello"]
splitters = ['my', 'is']
我希望输出为
final_list = ["hello ", " name ", " jolloopp", " jolloopp name ", " hello"]
注意空格。
也可以有类似的东西
draft_list = [["hello ", " name ", " jolloopp"], [" jolloopp name ", " hello"]]
可以使用 numpy reshape(-1,1)
之类的东西将其展平以获得 final_list
,但理想情况是
ideal_list = ["hello", "name", "jolloopp", "jolloopp name", "hello"]
空格被去掉的地方,类似于使用.strip()
.
编辑 1:
如果单词分隔符是其他单词的一部分,则使用 re.split
将无法完全发挥作用。
words = ["hellois my name is myjolloopp", "my isjolloopp name is myhello"]
splitters = ['my', 'is']
那么输出将是
['hello', '', 'name', '', 'jolloopp', '', 'jolloopp name', '', 'hello']
什么时候应该
['hellois', 'name', 'myjolloopp', 'isjolloopp name', 'myhello']
这是使用 re.split
的解决方案的已知问题。
编辑 2:
[x.strip() for x in re.split(' | '.join(splitters), ''.join(words))]
输入为
时无法正常工作
words = ["hello world", "hello my name is jolloopp", "my jolloopp name is hello"]
输出变为
['hello worldhello', 'name', 'jolloopp', 'jolloopp name', 'hello']
什么时候输出应该是
['hello world', 'hello', 'name', 'jolloopp', 'jolloopp name', 'hello']
你可以使用 re
比如,
使用@pault 建议的更好方式更新,使用单词边界 \b
而不是 :space:
、
>>> import re
>>> words = ['hello world', 'hello my name is jolloopp', 'my jolloopp name is hello']
# Iterate over the list of words and then use the `re` to split the strings,
>>> [z for y in (re.split('|'.join(r'\b{}\b'.format(x) for x in splitters), word) for word in words) for z in y]
['hello world', 'hello ', ' name ', ' jolloopp', '', ' jolloopp name ', ' hello']
我想要一种使用单词列表作为分隔符来拆分字符串列表的有效方法。输出是另一个字符串列表。
我在一行中尝试了多个 .split
,这不起作用,因为第一个 .split
returns 是一个列表,后面的 .split
需要一个字符串。
这是输入:
words = ["hello my name is jolloopp", "my jolloopp name is hello"]
splitters = ['my', 'is']
我希望输出为
final_list = ["hello ", " name ", " jolloopp", " jolloopp name ", " hello"]
注意空格。
也可以有类似的东西
draft_list = [["hello ", " name ", " jolloopp"], [" jolloopp name ", " hello"]]
可以使用 numpy reshape(-1,1)
之类的东西将其展平以获得 final_list
,但理想情况是
ideal_list = ["hello", "name", "jolloopp", "jolloopp name", "hello"]
空格被去掉的地方,类似于使用.strip()
.
编辑 1:
如果单词分隔符是其他单词的一部分,则使用 re.split
将无法完全发挥作用。
words = ["hellois my name is myjolloopp", "my isjolloopp name is myhello"]
splitters = ['my', 'is']
那么输出将是
['hello', '', 'name', '', 'jolloopp', '', 'jolloopp name', '', 'hello']
什么时候应该
['hellois', 'name', 'myjolloopp', 'isjolloopp name', 'myhello']
这是使用 re.split
的解决方案的已知问题。
编辑 2:
[x.strip() for x in re.split(' | '.join(splitters), ''.join(words))]
输入为
时无法正常工作words = ["hello world", "hello my name is jolloopp", "my jolloopp name is hello"]
输出变为
['hello worldhello', 'name', 'jolloopp', 'jolloopp name', 'hello']
什么时候输出应该是
['hello world', 'hello', 'name', 'jolloopp', 'jolloopp name', 'hello']
你可以使用 re
比如,
使用@pault 建议的更好方式更新,使用单词边界 \b
而不是 :space:
、
>>> import re
>>> words = ['hello world', 'hello my name is jolloopp', 'my jolloopp name is hello']
# Iterate over the list of words and then use the `re` to split the strings,
>>> [z for y in (re.split('|'.join(r'\b{}\b'.format(x) for x in splitters), word) for word in words) for z in y]
['hello world', 'hello ', ' name ', ' jolloopp', '', ' jolloopp name ', ' hello']