[Tutor] Newbie: Help on comparing files
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Tue Jan 13 17:04:22 EST 2004
On Tue, 13 Jan 2004, Terry Carroll wrote:
> On Mon, 12 Jan 2004, Isr Gish wrote:
>
> > This is the code I wrote to compare the files.
>
> Isr; I haven't tried running your code, but did you look into the
> difflib module to see if it did what you needed?
Hi Terry,
difflib won't work for Isr's problem, because difflib takes the order of
elements into account.
As a concrete example:
###
>>> s1 = '''
... a
... b
... c
... '''
>>> s2 = '''
... b
... a
... c
... '''
>>> import difflib
>>> diff = difflib.ndiff(s1, s2)
>>> print ''.join(diff)
+
+ b
a
- b-
c
###
According to Isr's problem statement, we shouldn't get any output, because
all the elements in 's1' exist in 's2'. So we can discount using
'difflib'.
However, there's still a module in the Standard Library that should be
very appropriate to Isr's question: the 'sets' module:
http://www.python.org/doc/lib/module-sets.html
###
>>> from sets import Set
>>> t1 = 'a b c d'
>>> t2 = 'c b d e'
>>> s1 = Set(t1.split())
>>> s2 = Set(t2.split())
>>> s1
Set(['a', 'c', 'b', 'd'])
>>> s2
Set(['c', 'b', 'e', 'd'])
>>> s1 - s2
Set(['a'])
>>> s2 - s1
Set(['e'])
###
Hope this clears up the situation!
More information about the Tutor
mailing list