我有以下列表:
[
['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']
]
基本上,我想做的是:
+G-left
,+A-left
,+G-right
和+A-right
的所有实例+G-left
或+A-left
,请向后看元素为+V
的列表的第一个实例,然后添加包含{{1}的列表的第一个索引 }或+G-left
到包含+A-left
或+V
标签的包含+G-left
的列表的末尾,然后继续并重复+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不够熟悉,还不知道它的语法。
答案 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
输出应该是您想要的。