, , ( collections.defaultdict), , . , collections.Counter. dict. , , , .
strings = ["cat", "act", "rat", "hut", "tar", "tact"]
anagrams = defaultdict(list)
for s in strings:
anagrams[frozenset(Counter(s).items())].append(s)
print([v for v in anagrams.values()])
print([x for v in anagrams.values() if len(v) > 1 for x in v])
Of course, if you prefer not to use the built-in functionality, you can use only a few lines, and also use the usual dictinstead defaultdictand write your own Counter, similar to what you have your method isanagram, just without part of the comparison.
source
share