我有一列令牌。我想为每个标记计数它所属的句子。如何获得以下结果?
input = ['This','is','the','first','sentence','。','Second','sentence','。'
desired_output = ['1','1','1','1','1','1','2','2','2']
或作为数字输入,其中每个点用1表示,其余点用0表示:
input = ['0','0','0','0','0''1','0','0''1']
答案 0 :(得分:0)
使用itertools.groupby
例如:
from itertools import groupby
data = ['1', '0', '0', '0', '1', '0', '0']
result = []
c = 1
for k, v in groupby(data):
if k == "0":
result.extend([str(c)]*len(list(v)))
c += 1
else:
result.append(k)
print(result)
输出:
['1', '1', '1', '1', '1', '2', '2']