2013-02-21 89 views
1

假设我在名为“main”的列表中有一串字符串。我如何通过迭代“主,如果我找到一个匹配的话,我删除匹配部分的‘主’,然后添加匹配的文本到一个所谓的新名单‘新’?将匹配追加到新列表并从原始列表中删除匹配

蟒蛇

main = ['text \fc + \fr this is my match1 \fc* text', 'text \fc + \fr this is my match2 \fc* text', 'text', 'text', 'text \fc + \fr this is my match \fc* text'] 
new = [] 

def rematch(pattern, inp): 
    matcher = re.compile(pattern) 
    matches = matcher.match(inp) 
    if matches: 
    new.append(matches) 
    #remove match from "main" somehow? 

for x in main: 
    for m in rematch('\\fc \+ \\fr(.*?)\\fc\*', x): 

结果:

main = ['text text', 'text text', 'text', 'text', 'text text'] 

new = ['this is my match1', 'this is my match2', 'this is my match3'] 

回答

2
In [33]: import re 

In [34]: pat = re.compile('\\fc \+ \\fr(.*?)\\fc\*') 

In [43]: main, new = zip(*[(''.join(parts[::2]), ''.join(parts[1::2])) for parts in [pat.split(m) for m in main]]) 

In [44]: new = [n.strip() for n in new if n] 

In [45]: main 
Out[45]: ('text text', 'text text', 'text', 'text', 'text text') 

In [46]: new 
Out[46]: ['this is my match1', 'this is my match2', 'this is my match'] 

说明:

注意当您使用pat.split会发生什么:

In [37]: pat.split(main[0]) 
Out[37]: ['text ', ' this is my match1 ', ' text'] 

这类似于你想,除了要在main奇数项和new偶条件是什么。我们会在一秒钟之内做到这一点。

首先,让我们应用pat.split到每个项目中main

In [51]: [pat.split(m) for m in main] 
Out[51]: 
[['text ', ' this is my match1 ', ' text'], 
['text ', ' this is my match2 ', ' text'], 
['text'], 
['text'], 
['text ', ' this is my match ', ' text']] 

接下来,让我们从偶数项分开奇品,并用''.join到斯马什项目连成一个字符串:

In [52]: [(''.join(parts[::2]), ''.join(parts[1::2])) for parts in [pat.split(m) for m in main]] 
Out[52]: 
[('text text', ' this is my match1 '), 
('text text', ' this is my match2 '), 
('text', ''), 
('text', ''), 
('text text', ' this is my match ')] 

从这里,我们可以使用zip(*...)分离mainnew

In [53]: main, new = zip(*[(''.join(parts[::2]), ''.join(parts[1::2])) for parts in [pat.split(m) for m in main]]) 

In [54]: main 
Out[54]: ('text text', 'text text', 'text', 'text', 'text text') 

In [55]: new 
Out[55]: (' this is my match1 ', ' this is my match2 ', '', '', ' this is my match ') 
+0

有没有办法做多个模式匹配? – user1898657 2013-02-21 15:31:01

+0

我不跟着你。你能详细说明你在找什么吗?也许有一个例子? – unutbu 2013-02-21 15:33:34

+0

我想通了,我感谢这个精心制作和美丽的解决方案:) – user1898657 2013-02-21 15:34:46