[Tutor] Trying tio emulate "diff" command of UNIX - please help

Asrarahmed Kadri ajkadri at googlemail.com
Fri Oct 6 21:27:15 CEST 2006


Thanks Alan, but can you please explain me what this line does:
diff = [t1==t2 for t1,t2 in zip(line1,line2)].index(False)




On 10/6/06, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> Some general thoughts:
>
> > import sys
> > from stringCompare import stringcmp   # this is a module which has
> > stringcmp
> >
> > fname1 = raw_input("Enter a file name to be read:\t")
> > fname2 = raw_input("Enter a file name to be read:\t")
> >
> > fd1 = open(fname1,"r")
> > fd2 = open(fname2,"r")
> >
> >
> > done = False
>
> Use Boolean value instead of 1/0
>
> > line_counter = 0
> > while not done:
> >    aLine1 = fd1.readline()
> >    aLine2 = fd2.readline()
> >
> >    if (aLine1 == "" or aLine2 == ""):  # test whether you have
> > reached the
> > end of file
> >        done = 1
>
> assign directly to done, using a boolean test:
>
> done = not (aLine1 and aLine2)
>
> and miss out the if/else test.
>
> >        line_counter += 1                 # get the line number
> >        string1 = aLine1.split()         # split the line into a
> > list containing words
> >        string2 = aLine2.split()
>
> Not sure why you are splitting the lines into words.
> You call them columns but they are really words of
> varying length. Why not just compare the lines as a whole?
>
> if aLine1 != aLine2:
>     print "The difference lies in line", line_counter
>
> Then you can find the first characters that differ:
>
>     diff = [t1==t2 for t1,t2 in zip(line1,line2)].index(False)
>     print "and start at character", diff
>
> It's not exactly equivalent to your code of course but it seems to me
> to be more accurate...
>
> If you must use words, apply the spolit only when you know its needed:
>
>     words1 = aLine1.split()
>     words2 = aLine2.split()
>     diff = [w1==w2 for w1,w2 in zip(words1,words2)].index(False)
>     print "and start at word",diff
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
To HIM you shall return.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061006/b6cce2cc/attachment.html 


More information about the Tutor mailing list