Checking if two strings are permutations of each other in Python

前端 未结 22 2508
旧时难觅i
旧时难觅i 2020-12-08 16:04

I\'m checking if two strings a and b are permutations of each other, and I\'m wondering what the ideal way to do this is in Python. From the Zen of

22条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 16:32

    This version is faster than any examples presented so far except it is 20% slower than sorted(x) == sorted(y) for short strings. It depends on use cases but generally 20% performance gain is insufficient to justify a complication of the code by using different version for short and long strings (as in @patros's answer).

    It doesn't use len so it accepts any iterable therefore it works even for data that do not fit in memory e.g., given two big text files with many repeated lines it answers whether the files have the same lines (lines can be in any order).

    def isanagram(iterable1, iterable2):
        d = {}
        get = d.get
        for c in iterable1:
            d[c] = get(c, 0) + 1
        try:
            for c in iterable2:
                d[c] -= 1
            return not any(d.itervalues())
        except KeyError:
            return False
    

    It is unclear why this version is faster then defaultdict (@namin's) one for large iterable1 (tested on 25MB thesaurus).

    If we replace get in the loop by try: ... except KeyError then it performs 2 times slower for short strings i.e. when there are few duplicates.

提交回复
热议问题