Python sets which support multiple same elements

Discussion in 'Python' started by ErichCart ErichCart, May 20, 2011.

  1. Many times when I am writing some program in python, I notice that I
    could transform my list into set, then use the set methods like union,
    intersection, set equality etc. , and it will solve my problem easily.
    But then I realize that if I transform my list into set, it will
    remove duplicates of elements in the list and so I will lose
    information from my original list.

    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

    But if there was a data structure in python which supported duplicate
    elements(lets call it dset), then I could just write:

    def inAnagram(w1,w2):
    return dset(w1)==dset(w2)

    Example of some dset methods:
    {1,2,3,3} intersection {4,1,2,3,3,3} == {1,2,3,3}
    {1,2,3,3} union {4,1,2,3,3,3} == {1,2,3,3,3,4}
    {4,1,2,3,3,3} difference {1,2,3,3} == {4,3}

    Do you think that it would be a good idea to add this kind of data
    structure to python? Or did I overlook some other easy way to solve
    this kind of problems?
     
    ErichCart ErichCart, May 20, 2011
    #1
    1. Advertising

  2. ErichCart ErichCart

    Andreas Tawn Guest

    > 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
    >
    > But if there was a data structure in python which supported duplicate
    > elements(lets call it dset), then I could just write:
    >
    > def inAnagram(w1,w2):
    > return dset(w1)==dset(w2)
    >
    > Example of some dset methods:
    > {1,2,3,3} intersection {4,1,2,3,3,3} == {1,2,3,3}
    > {1,2,3,3} union {4,1,2,3,3,3} == {1,2,3,3,3,4}
    > {4,1,2,3,3,3} difference {1,2,3,3} == {4,3}
    >
    > Do you think that it would be a good idea to add this kind of data
    > structure to python? Or did I overlook some other easy way to solve
    > this kind of problems?


    Just to do the anagram problem you could do...

    def isAnagram(w1, w2):
    return sorted(w1) == sorted(w2)

    To do the set-like operations, I guess that unless there's some itertools witchcraft available, you'd have to make your own dset type that inherits from list. Then you can define your own intersection/union etc. methods.

    Cheers,

    Drea
     
    Andreas Tawn, May 20, 2011
    #2
    1. Advertising

  3. On Fri, May 20, 2011 at 9:37 PM, ErichCart ErichCart
    <> 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
     
    Chris Angelico, May 20, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arby Trary
    Replies:
    6
    Views:
    2,859
    Steve Pugh
    Jan 31, 2005
  2. Replies:
    3
    Views:
    291
    wes weston
    Apr 8, 2005
  3. wheel
    Replies:
    3
    Views:
    303
    wheel
    Feb 18, 2006
  4. Iain Wilson
    Replies:
    1
    Views:
    291
    Eliyahu Goldin
    Apr 17, 2008
  5. Iain Wilson
    Replies:
    2
    Views:
    357
    Iain Wilson
    Apr 18, 2008
Loading...

Share This Page