[Tutor] Newbie: Help on comparing files
Isr Gish
isrgish at fusemail.com
Tue Jan 13 23:52:54 EST 2004
Thanks Danny, I'll try this.
-----Original Message-----
>From: "Danny Yoo"<dyoo at hkn.eecs.berkeley.edu>
>Sent: 1/13/04 5:04:22 PM
>To: "Terry Carroll"<carroll at tjc.com>
>Cc: "tutor at python.org"<tutor at python.org>
>Subject: Re: [Tutor] Newbie: Help on comparing files
>
>
>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!
>
Thanks to all,
Isr
More information about the Tutor
mailing list