繁体   English   中英

Python比较两个字符串

[英]Python comparing two strings

有没有一个函数可以比较两个(相同长度)字符串之间有多少个字符不同? 我的意思是只换人。 例如,AAA与AAT的区别是1个字符。

这将起作用:

>>> str1 = "AAA"
>>> str2 = "AAT"
>>> sum(1 for x,y in enumerate(str1) if str2[x] != y)
1
>>> str1 = "AAABBBCCC"
>>> str2 = "ABCABCABC"
>>> sum(1 for x,y in enumerate(str1) if str2[x] != y)
6
>>>

上面的解决方案使用sumenumerategenerator表达式


因为True可以求值为1 ,所以您甚至可以执行以下操作:

>>> str1 = "AAA"
>>> str2 = "AAT"
>>> sum(str2[x] != y for x,y in enumerate(str1))
1
>>>

但是我个人更喜欢第一个解决方案,因为它更清晰。

这是zip功能的一个很好的用例!

def count_substitutions(s1, s2):
    return sum(x != y for (x, y) in zip(s1, s2))

用法:

>>> count_substitutions('AAA', 'AAT')
1

从文档:

zip(...)
    zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

    Return a list of tuples, where each tuple contains the i-th element
    from each of the argument sequences.  The returned list is truncated
    in length to the length of the shortest argument sequence.

根据p说的内容,我建议您使用水母包装。 它具有几种您想要的距离测量方法。 文档中的示例:

IN [1]: jellyfish.damerau_levenshtein_distance('jellyfish', 'jellyfihs')
OUT[1]: 1

或使用您的示例:

IN [2]: jellyfish.damerau_levenshtein_distance('AAA','AAT')
OUT[2]: 1

这将适用于许多不同的字符串长度,并且应该能够处理您扔给它的大部分内容。

与simon的答案类似,但您不必为了在结果元组上调用函数而压缩所有内容,因为无论如何,这就是map功能(以及Python 2中的itertools.imap )。 并且operator有一个方便的!=功能。 因此:

sum(map(operator.ne, s1, s2))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM