我正在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))
谢谢!任何帮助将不胜感激!
答案 0 :(得分: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)]
适当格式化......