简体   繁体   English

在python中用多个匹配项划分字符串

[英]Dividing the string with multiple matches in python

I have a string that has to be split for words that are present in "words"我有一个字符串,必须为“单词”中存在的单词拆分

words = ['word1', 'word2', 'word3']
text = " long statement word1 statement1 word2 statement2 word3 statement3 "  # a single lined string

The code I'm using, is there any simple way for this?我正在使用的代码,有什么简单的方法吗?

  for l in words:
        if l == "word1": t1 = text.split(l)
        if l == "word2": t2 = str(t1[1]).split(l)
        if l == "word3": t3 = str(t2[1]).split(l)
    
    print(t1[0])
    print(t2[0])
    print(t3[0])

The output is like:输出是这样的:

statement
statement1
statement2
statement3

How about using itertools.如何使用itertools。 groupby : 分组

from itertools import groupby

words = ['word1', 'word2', 'word3']
text = " long statement word1 statement1 word2 statement2 word3 statement "
delimiters = set(words)
statements = [
    ' '.join(g) for k, g in groupby(text.split(), lambda w: w in delimiters)
    if not k
]
print(statements)

Output:输出:

['long statement', 'statement1', 'statement2', 'statement3']

You could Regex for solving your problem in this way.您可以通过 Regex 以这种方式解决您的问题。

import re

words = ['word1', 'word2', 'word3']
text = " long statement word1 statement1 word2 statement2 word3 statement3 "
    
print(*re.split('|'.join(words),text), sep="\n")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM