list comprehention

Peter Otten __peter__ at web.de
Tue Jan 24 03:46:29 EST 2006


[Mathijs]

> Example2:
> ref=[2, 2, 4, 1, 1]
> list=[2, 2, 5, 2, 4]
> solution: 3 (note that only the first two 2's count, the third 2 in the 
> list should not be counted)

[Morten Vold]

> May I suggest another one-liner:
> 
>  len(set(ref).intersection(lis))
> 
> I have no idea how this scales/performs compared to the other
> suggestions you've received, but I find it immediately comprehensible.

>>> len(set([2, 2, 4, 1, 1]).intersection([2, 2, 5, 2, 4]))
2

Close, but no cigar.

Peter



More information about the Python-list mailing list