2

我正在用python创建一个程序,它将遍历句子列表并在句子中找到大写的单词。我现在使用 findall 函数来获取首都。

这是我现在收到的输出示例:

line 0: the dog_SUBJ bit_VERB the cat_OBJ
['S'] ['U'] ['B'] ['J'] [] ['V'] ['E'] ['R'] ['B'] [] ['O'] ['B'] ['J'] 

但是,我希望输出是完整的单词,如下所示:

['SUBJ'] [] ['VERB'] [] ['OBJ']

我也想要单词的索引:

['SUBJ'] [0]
['VERB'] [1]
['OBJ'] [2]

是否有可能做到这一点?我之前在终端上看到过上述操作,我认为使用了“索引”或类似的东西?

下面是我的代码(据我所知):

import re, sys
f = open('findallEX.txt', 'r')
lines = f.readlines()
ii=0
for l in lines:
    sys.stdout.write('line %s: %s' %(ii, l))
    ii = ii + 1
    results = []
    for s in l:
        results.append(re.findall('[A-Z]+', s))

谢谢!任何帮助将不胜感激!

4

1 回答 1

2

就像是:

>>> s = 'the dog_SUBJ bit_VERB the cat_OBJ'
>>> import re
>>> from itertools import count
>>> zip(re.findall('[A-Z]+', s), count())
[('SUBJ', 0), ('VERB', 1), ('OBJ', 2)]

适当的格式...

于 2013-04-22T11:36:25.463 回答