comparing two lists and returning "position"

Charles Sanders C.delete_this.Sanders at BoM.GOv.AU
Fri Jun 22 01:46:52 EDT 2007


Paul Rubin wrote:
> 
> from itertools import izip
> pos = map(dict(izip(l2, count())).__getitem__, l1)

or probably less efficiently ...

 >>> l1 = [ 'abc', 'ghi', 'mno' ]
 >>> l2 = [ 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqr']
 >>> pos = [ l2.index(i) for i in l1 ]
 >>> print pos
[0, 2, 4]

Charles



More information about the Python-list mailing list