Python sets which support multiple same elements

Chris Angelico rosuav at gmail.com
Fri May 20 07:57:44 EDT 2011


On Fri, May 20, 2011 at 9:37 PM, ErichCart ErichCart
<erichcart at gmail.com> wrote:
> For example, I was writing a program to detect whether two strings are
> anagrams of each other. I had to write it like this:
>
> def isAnagram(w1, w2):
>  w2=list(w2)
>  for c in w1:
>    if c not in w2:
>      return False
>    else:
>      w2.remove(c)
>  return True

You may find it helpful to simply sort the lists, keeping them as
lists. If they're anagrams of each other, their sorted versions will
be equal.

Chris Angelico



More information about the Python-list mailing list