【发布时间】:2014-03-02 19:20:54
【问题描述】:
我的目标是在两个对齐的文本文档中识别匹配的字符串,然后在每个文档中找到匹配字符串的起始字符的位置。
doc1=['the boy is sleeping', 'in the class', 'not at home']
doc2=['the girl is reading', 'in the class', 'a serious student']
我的尝试:
# find matching string(s) that exist in both document list:
matchstring=[x for x in doc1 if x in doc2]
Output=matchstring='in the class'
'
现在的问题是在 doc1 和 doc2 中查找匹配字符串的字符偏移量(不包括标点符号,包括空格)。
理想结果:
Position of starting character for matching string in doc1=20
Position of starting character for matching string in doc2=20
关于文本对齐的任何想法?谢谢。
【问题讨论】:
-
为什么我发现它是 19 而不是 21?
-
嗨@zhangxaochen,你在'sleeping'中停止计数'g'而不是'in the class'中的'i'。
-
“男孩正在睡觉”的长度为 19,
i是第 20 个字符,如果从 0 开始索引,则位于位置 19。 -
你是对的'如果从零开始索引',那么字符偏移量是第 20 个字符。请问我可以看看你的方法吗?
-
@zhangxaochen,你能告诉我你是怎么做的吗?任何人都可以通过主要查看索引。