用两个单词拆分字符串

时间:2018-04-15 10:05:25

标签: python

有没有办法在两个分隔符中分割字符串。

代码示例:

# word --> u1 or word --> u2
a = "Hi thereu1hello ?u1Whatu2Goodu1Work worku2Stacku2"
# here we must split this string with two words "u1" && "u2" and insert them in 2 list like this
u1 = ["Hi there", "hello ?", "Good"]
u2 = ["What", "Work work", "Stack"]

2 个答案:

答案 0 :(得分:2)

您可以按字符顺序迭代字符串并在text: { position: 'absolute', fontSize: 60, left: 0, top: 0, } 列表中累积字符,直到该列表中的最后一个字符为part,并且您当前的字符为'u'或{{1 }}。

然后,您再次加入'1' - 列表,省略其最后一个字符('2'),并将其填入part'u'并清除{{1 }}:

u1

输出:

u2

第二种方法是记住某些索引(在最后一个切片结束时,我们现在在哪里)并从输入中切出正确的部分:

part

你不需要那么多"记忆/时间"存储/附加到a = "Hi thereu1hello ?u1Whatu2Goodu1Work worku2Stacku2" u1 = [] u2 = [] part = [] # iterate your string character-wise for c in a: # last character collected == u and now 1 or 2? if part and part[-1] == "u" and c in ["1","2"]: if c == "1": u1.append(''.join(part[:-1])) # join all collected chars, omit 'u' part=[] else: u2.append(''.join(part[:-1])) # see above, same. part=[] else: part.append(c) # you have no end-condition if your string ends on neither u1 nor u2 the # last part of your string is not added to any u1 or u2 print(u1) print(u2) 列表时,您可以根据需要存储单个整数和一个字符 - 当您直接从源切片时,您也不需要加入['Hi there', 'hello ?', 'Good'] ['What', 'Work work', 'Stack'] 进行追加 - 所以它稍微效率更高。

答案 1 :(得分:1)

您可以使用正则表达式来实现一个简单的可扩展解决方案:

import re

a = "Hi thereu1hello ?u1Whatu2Goodu1Work worku2Stacku2"
separators = ['u1', 'u2']

regex = r'(.*?)({})'.format('|'.join(re.escape(sep) for sep in separators))
result = {sep: [] for sep in separators}
for match in re.finditer(regex, a, flags=re.S):
    text = match.group(1)
    sep = match.group(2)
    result[sep].append(text)

print(result)
# {'u1': ['Hi there', 'hello ?', 'Good'],
#  'u2': ['What', 'Work work', 'Stack']}

这样就可以构建分隔符u1u2中的正则表达式,如下所示:

(.*?)(u1|u2)

然后迭代这个正则表达式的所有匹配并将它们附加到相应的列表。