Comparing modified elements in Sets

Discussion in 'Python' started by ChrisEdgemon@gmail.com, Jul 9, 2007.

  1. Guest

    I've got a set with contents like: [filename1.mp3, filename2.mp3,
    filename3.mp3, ...]
    I've got another set with contents like [filename1.ogg,
    filename3.ogg, ...]
    And another set with contents like [filename1, filename 2, ...]

    I'd like to be able to compare set 1 with set 2 and have it match
    filename1 and filename3, or compare set 1 with 3 and get back
    filename1, filename2. etc.

    Is there a way for me to do this inside the compare function, rather
    than having to make duplicate copies of each set?
    , Jul 9, 2007
    #1
    1. Advertising

  2. Guest

    On Jul 9, 2:57 pm, wrote:
    > I've got a set with contents like: [filename1.mp3, filename2.mp3,
    > filename3.mp3, ...]
    > I've got another set with contents like [filename1.ogg,
    > filename3.ogg, ...]
    > And another set with contents like [filename1, filename 2, ...]
    >
    > I'd like to be able to compare set 1 with set 2 and have it match
    > filename1 and filename3, or compare set 1 with 3 and get back
    > filename1, filename2. etc.
    >
    > Is there a way for me to do this inside the compare function, rather
    > than having to make duplicate copies of each set?


    <A HREF="resumepage.html">my resume</A
    , Jul 10, 2007
    #2
    1. Advertising

  3. On Mon, 09 Jul 2007 19:57:14 +0000, ChrisEdgemon wrote:

    > I've got a set with contents like: [filename1.mp3, filename2.mp3,
    > filename3.mp3, ...]
    > I've got another set with contents like [filename1.ogg,
    > filename3.ogg, ...]
    > And another set with contents like [filename1, filename 2, ...]
    >
    > I'd like to be able to compare set 1 with set 2 and have it match
    > filename1 and filename3, or compare set 1 with 3 and get back
    > filename1, filename2. etc.
    >
    > Is there a way for me to do this inside the compare function, rather
    > than having to make duplicate copies of each set?


    What compare function are you talking about?

    Unless you have truly astronomical numbers of unique filenames (and
    remember that what's big to you is probably not big to Python), the
    easiest way is to make a copy of each set with the file extensions
    stripped off the file names. Use os.path.splitext() for that.


    --
    Steven.
    Steven D'Aprano, Jul 10, 2007
    #3
  4. Asun Friere Guest

    On Jul 10, 5:57 am, wrote:
    > I'd like to be able to compare set 1 with set 2 and have it match
    > filename1 and filename3, or compare set 1 with 3 and get back
    > filename1, filename2. etc.
    >
    > Is there a way for me to do this inside the compare function, rather
    > than having to make duplicate copies of each set?


    Is there a will?
    Inevitably there is a way! Whether you should take it is another
    question entirely. ;)

    Assuming by 'compare' function you mean such methods as 'difference,'
    'symetric_difference', 'intersection' and the like... here's a nasty
    little hack (using the old-school Set from sets.py) It's not to spec
    (you get the tails back in the result, but that's easily fixed), and
    it only implements a replacement method for 'difference' (called
    'tailess_difference).

    I apologise if the google groups mailer kludges the indentation ...
    -----
    from sets import Set
    from itertools import ifilterfalse
    from os.path import splitext

    class BodgySet (Set) :

    def tailess_difference (self, other) :
    """Return, as a new BodgySet, the difference of two
    sets, where element identity ignores all characters
    from the last stop (period).

    NOTE: As currently implemented all elements of said
    sets must be strings (fix this in self.has_key)!!!

    """
    assert other.__class__ is self.__class__
    result = self.__class__()
    data = result._data
    value = True
    for elt in ifilterfalse(other.has_key, self) :
    data[elt] = value
    return result

    def has_key (self, target) :
    thead, ttail = splitext(target)
    for key in self._data.keys() :
    khead, ktail = splitext(key)
    if thead == khead :
    return True
    -----

    Using this hacked set:
    >>> a = BodgySet(['a1.txt', 'a2.txt'])
    >>> b = BodgySet(['a1.xml', 'a2.xml', 'a3.xml'])
    >>> b.tailess_difference(a)

    BodgySet(['a3.xml'])

    Is that the kind of thing you had in mind?

    While it can be done, I would prefer to make copies of the sets, with
    a cast list comprehension something like: set([os.path.splitext(x)[0]
    for x in orig_set]). Much better readibility and probably greater
    efficiency (I haven't bothered timing or dissing it mind).
    Asun Friere, Jul 10, 2007
    #4
    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. Doug

    Comparing node sets

    Doug, Jul 25, 2003, in forum: XML
    Replies:
    1
    Views:
    497
    Marrow
    Jul 25, 2003
  2. Andy Fish

    xpath: comparing two node sets

    Andy Fish, Mar 9, 2005, in forum: XML
    Replies:
    3
    Views:
    2,732
    Andy Fish
    Mar 10, 2005
  3. Replies:
    11
    Views:
    448
    Mike Meyer
    Jan 12, 2006
  4. John Salerno

    comparing values in two sets

    John Salerno, May 14, 2006, in forum: Python
    Replies:
    11
    Views:
    555
    Gerard Flanagan
    May 15, 2006
  5. Terry L. Ridder
    Replies:
    4
    Views:
    124
    Quantum Mechanic
    Oct 14, 2003
Loading...

Share This Page