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

ALAN GAULD alan.gauld at btinternet.com
Sat Oct 7 00:21:09 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)
> 

I'll unfold it somewhat:

    mix = zip(a,b)   

produces a list of tuples:
[(a1,b1)(a2,b2),(a3,b3)]

    t1 == t2 

produces a boolean result either True or False

    for t1,t2 in mix

unpacks each tuple from the zipped list

Putting it back together in long hand

mix = zip(aLine1,aLine2)
results = []
for t1,t2 in mix:
   if t1 == t2:
      results.append(True)
   else: results.append(False)

diff = results.index(False)

finds the index of the first instance of False 
in results

Lets look at it as a comprehension again:

diff = [t1==t2 for t1,t2 in zip(aLine1,aLine2)].index(False)

Does that make sense now? Can you see the various 
bits at work?

Alan G.



		
___________________________________________________________ 
To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre. http://uk.security.yahoo.com


More information about the Tutor mailing list