gpt4 book ai didi

python - 将匹配项添加到新列表并从原始列表中删除匹配项

转载 作者:行者123 更新时间:2023-12-01 05:49:17 26 4
gpt4 key购买 nike

假设我在名为“main”的列表中有一堆字符串。如何迭代“main”,如果找到匹配项,则删除“main”中的匹配部分,然后将匹配的文本添加到名为“new”的新列表中?

python

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']

最佳答案

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 ')

关于python - 将匹配项添加到新列表并从原始列表中删除匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15005734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com