说我有这张单子
jay = ['Despite', 'similar', 'intensity', 'of', 'alcohol', '<Disease:D013375>', 'withdrawal', 'symptoms', '</Disease:D013375>', ',', 'ALC', '/', 'COC', 'subjects', 'received', 'less', 'oxazepam', 'to', 'treat', 'alcohol', '<Disease:D013375>', 'withdrawal', 'symptoms', '</Disease:D013375>', 'compared', 'to', 'ALC', 'subjects', '.']
我想做一个与原来的列表相对应的新列表。如果一个项目位于项目'<Disease:XXXXX>'
和'</Disease:XXXXX>'
之间,则第一个项目将被标记为“B-COL”,其余项目将被标记为“I-COL”。你知道吗
项'<Disease:XXXXX>'
和'</Disease:XXXXX>'
本身没有任何标记。XXXX可以是数字范围。你知道吗
所有其他项目都被标记为“O”。你知道吗
下面是我想要的输出示例。你知道吗
idealOutput= ['O', 'O', 'O', 'O', 'O', 'B-COL', 'I-COL', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-COL', 'I-COL', 'O', 'O', 'O', 'O', 'O']
“疾病”标签对的数量可能会有所不同,这些标签之间的项目数量也会有所不同。你知道吗
以下是我的尝试:
wow = jay
labs = []
for i in range(0, len(wow)):
if wow[i].startswith("<Disease"):
labs.append('DelStrB')
elif i>0 and i<=len(labs):
if labs[i-1] == 'DelStrB':
labs.append('B-COL')
i = i + 1
while not (wow[i].startswith("</Disease")):
labs.append('I-COL')
i = i + 1
if wow[i].startswith("</Disease"):
labs.append('DelStrE')
i = i + 1
elif wow[i].startswith("</Disease"):
k=9 #do nothing
else:
labs.append('O')
elif wow[i].startswith("</Disease"):
k=9 #do nothing
else:
labs.append('O')
labs[:] = [x for x in labs if x != 'DelStrB']
labs[:] = [x for x in labs if x != 'DelStrE']
print(labs)
结果是
['O', 'O', 'O', 'O', 'O', 'B-COL', 'I-COL', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-COL', 'O', 'O', 'O', 'O', 'O']
这是不正确的。我也知道有一种计算效率更高、更优雅的编码方式,但无法产生它。你知道吗
可以使用简单的生成器:
输出:
使用迭代方法的解决方案:
您可以使用
itertools.groupby
和一个查找“疾病”项的键函数,将列表分成奇数和偶数两组,以便使用不同的标记方法:这将返回:
请注意,预期的输出是不正确的,因为它在
'B-COL'
和'I-COL'
的两个序列之间还有2个'O'
相关问题 更多 >
编程相关推荐