在列表列表中向后查找第一个匹配项

时间:2020-09-21 17:02:53

标签: python regex list nlp

我有以下列表:

[
['the', 'the +Det'],
['dog', 'dog +N +A-right'],
['ran', 'run +V +past'],
['at', 'at +P'], 
['me', 'I +N +G-left'],
['and', 'and +Cnj'],
['the', 'the +Det'],
['ball', 'ball +N +G-right'],
['was', 'was +C'],
['kicked', 'kick +V +past']
['by', 'by +P']
['me', 'I +N +A-left']

]

基本上,我想做的是:

  1. 反复浏览列表
  2. 找到+G-left+A-left+G-right+A-right的所有实例
  3. 如果看到+G-left+A-left,请向后看元素为+V的列表的第一个实例,然后添加包含{{1}的列表的第一个索引 }或+G-left到包含+A-left+V标签的包含+G-left 的列表的末尾,然后继续并重复
  4. 如果看到+A-left+G-right,请期待元素为+A-right的列表的第一个实例,然后添加包含{{1}的列表的第一个索引 }或+V到包含+G-right+A-right标签的+V 列表的末尾,然后继续并重复

因此,在我上面的示例中,所需状态为:

+G-right

我认为正确的方法是使用+A-right,所以:

[
['the', 'the +Det'],
['dog', 'dog +N +A-right'],
['ran', 'run +V +past', 'dog+A-right', 'me+G-left'],
['at', 'at +P'], 
['me', 'I +N +G-left'],
['and', 'and +Cnj'],
['the', 'the +Det'],
['ball', 'ball +N +G-right'],
['was', 'was +C'],
['kicked', 'kick +V +past', 'ball+G-right', 'me+A-left']
['by', 'by +P']
['me', 'I +N +A-left']
]

然后类似

re

也是一样,但带有G标签。

希望有人可以帮助我指出正确的方向。我相信我已经正确地分解了步骤,只是我对Python不够熟悉,还不知道它的语法。

1 个答案:

答案 0 :(得分:1)

使用辅助功能可以稍微简化一下,但除此之外,请尝试此操作,不需要正则表达式:

wls = [your list of lists, above, fixed (some commas are missing)]
for wl in wls:
    for w in wl:
        if '-right' in w:                        
            targ = wls.index(wl)            
            counter = 0
            for wt in (wls[targ+1:]):                               
                for t in wt:
                    if '+V' in t:
                        if counter<1:                            
                            wt.insert(len(wt),wl[0]+w.split(' ')[-1])
                        counter+=1

        if '-left' in w:            
            targ = wls.index(wl)            
            counter = 0
            revd = [item for item in reversed(wls[:targ])]
            for wt in revd:           
                for t in wt:
                    if '+V' in t:
                        if counter<1:
                            wt.insert(len(wt),wl[0]+w.split(' ')[-1])
                        counter+=1
           
wls

输出应该是您想要的。

相关问题