我的代码是:
for i in range(0,len(mylist1)):
dot=str(i)+'.'
print dot , mylist1[i]
if dot in mylist1:
print "find"
mylist1[i+1]='RD_PUNC'
mylist1=['1.alen','N_NN','2.','N_NP','3.abr','N_NNP','4.london','N_NST','5.','N_NNP']
我想找到2,4。,任何数字后跟'。'并将列表中的下一项更改为“RD_PUNC” 我想要的输出是:
mylist1=['1.alen','N_NN','2.','RD_PUNC','3.abr','N_NNP','4.london','N_NST','5.','RD_PUNC']
答案 0 :(得分:1)
使用itertools:
from itertools import izip
import re
mylist1=['1.alen','N_NN','2.','RD_PUNC','3.abr','N_NNP','4.london','N_NST','5.','RD_PUNC']
newList = []
def pairwise(iterable):
a = iter(iterable)
return izip(a, a)
replaceX = False
for x, y in pairwise(mylist1):
if replaceX:
x = 'RD_PUNC'
replaceX = False
elif re.match(r'\d+\.$', x):
y = 'RD_PUNC'
if re.match(r'\d+\.$', y):
replaceX = True
newList.append(x)
newList.append(y)
print newList
输出:
['1.alen', 'N_NN', '2.', 'RD_PUNC', '3.abr', 'N_NNP', '4.london', 'N_NST', '5.', 'RD_PUNC']