[Tutor] Matching relational data

Steven D'Aprano steve at pearwood.info
Mon Oct 4 18:08:17 CEST 2010


On Tue, 5 Oct 2010 02:04:09 am David Hutto wrote:

> a = ['+','-','+','-','+','-','+','-','+','-','+']
> b = ['-','+','-','+','-','-','-','+','-','-','-']
>
> count = 0
> lena = len(a)
> lenb = len(b)
> if lena == lenb:
> 	for num in range(0,lena):
> 		if a[num] == b[num]:
> 			print 'match'
> 			count += 1
> else:
> 	print 'Lists are not same length for comparison'
> per = (100/lena)
> print count * per, '% match'


a = '+-+-+-+-+-+'
b = '-+-+---+---'
if len(a) != len(b):
    raise ValueError('lists are not the same length')
count = sum(ca == cb for (ca, cb) in zip(a, b))




-- 
Steven D'Aprano


More information about the Tutor mailing list