Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Comparing modified elements in Sets

Reply
Thread Tools

Comparing modified elements in Sets

 
 
ChrisEdgemon@gmail.com
Guest
Posts: n/a
 
      07-09-2007
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?

 
Reply With Quote
 
 
 
 
dabu11et@rock.com
Guest
Posts: n/a
 
      07-09-2007
On Jul 9, 2:57 pm, (E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-09-2007
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.

 
Reply With Quote
 
Asun Friere
Guest
Posts: n/a
 
      07-10-2007
On Jul 10, 5:57 am, (E-Mail Removed) 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).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing values in two sets John Salerno Python 11 05-15-2006 11:25 AM
How can I create a dict that sets a flag if it's been modified sandravandale@yahoo.com Python 11 01-12-2006 05:18 PM
xpath: comparing two node sets Andy Fish XML 3 03-10-2005 09:16 AM
suggestions for comparing two large data sets requested Terry L. Ridder Perl Misc 4 10-14-2003 10:28 PM
Comparing node sets Doug XML 1 07-25-2003 03:02 PM



Advertisments