【发布时间】:2018-02-12 00:15:57
【问题描述】:
我想在一个列表中记录两个字符串的差异位置(以删除它们)...最好记录每个部分的最高分隔点,因为这些区域将具有动态内容。
比较这些
总字符数 178。两个独特的部分
t1 = 'WhereTisthetotalnumberofght5y5wsjhhhhjhkmhm Thethreemethodsthatreturntheratioofmatchingtototalcharacterscangivedifferentresultsduetodifferinglevelsofapxxxxxxxproximation,although'
和
总字符数 211。两个独特的部分
t2 = 'WhereTisthetotalnumberofdofodfgjnjndfgu><rgregw><sssssuguyguiygis>gggs<GS,Gs Thethreemethodsthatreturntheratioofmatchingtototalcharacterscangivedifferentrexxxxxxxsultsduetodifferinglevelsofapproximation,although'
我知道 difflib 可以做到这一点,但输出很糟糕。
我想(在列表中)存储字符位置,最好是较大的分隔值。
图案位置
t1 = 'WhereTisthetotalnumberof 24 ght5y5wsjhhhhjhkmhm 43 Thethreemethodsthatreturntheratioofmatchingtototalcharacterscangivedifferentresultsduetodifferinglevelsofap 151 xxxxxxx 158 proximation,although'
t2 = 'WhereTisthetotalnumberof 24 dofodfgjnjndfgu><rgregw><sssssuguyguiygis>gggs<GS,Gs 76 Thethreemethodsthatreturntheratioofmatchingtototalcharacterscangivedifferentre 155 xxxxxxx 162 sultsduetodifferinglevelsofapproximation,although'
输出:
output list = [24, 76, 151, 162]
更新
回复@Olivier
由 *** 分隔的所有 Y 的位置
t1
WhereTisthetotalnumberofght5***y***5wsjhhhhjhkmhm Thethreemethodsthatreturntheratioofmatchingtototalcharacterscangivedifferentresultsduetodifferinglevelsofapxxxxxxxproximation,although
t2 WhereTisthetotalnumberofdofodfgjnjndfgu><rgregw><sssssugu***y***gui***y***gis>gggs<GS,Gs Thethreemethodsthatreturntheratioofmatchingtototalcharacterscangivedifferentrexxxxxxxsultsduetodifferinglevelsofapproximation,although
matcher.get_matching_blocks()之后的输出
和string = ''.join([t1[a:a+n] for a, _, n in blocks])
WhereTisthetotalnumberof***y*** Thethreemethodsthatreturntheratioofmatchingtototalcharacterscangivedifferentresultsduetodifferinglevelsofapproximation,although
【问题讨论】:
-
除了它的减法和加法值放在字符旁边。它只处理非常慢的列表。这些字符串会很大。 2000 多个字符。但是欢迎使用 difflib 示例进行基准测试。谢谢!
标签: string python-3.x difflib