[Tutor] Difference

Andrei Kulakov ak@silmarill.org
Mon, 12 Nov 2001 16:23:21 -0500


On Mon, Nov 12, 2001 at 02:03:52PM -0700, Mike Yuen wrote:
> I'm wondering if there's a simple function that will return the difference
> between two lists OR dictionary items.
> 
> For example, say i've got the following:
> A: [a, b, c, d, e, f, g, h, i]
> B: [a, b, d, c, e, g, f, i, h]
> 
> I would like to know if there's a comparison function that will tell me
> there are 6 differences between lists A,B
> 
> Thanks,
> M

Well, I don't think there is one but you can write it..

def diff(a, b):
    if type(a) == type({}):
        a = a.keys()
        b = b.keys()
    d = 0
    for i in range(len(a)):
        if a[i] != b[i]:
            d += 1
    print "%d differences between a and b" % d
        
A= "a, b, c, d, e, f, g, h, i".split(',')
B= "a, b, d, c, e, g, f, i, h".split(',')

diff(A, B)

6 differences between a and b

> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org