comparing two lists and returning "position"

hiro NunezD at gmail.com
Fri Jun 22 02:16:59 EDT 2007


On Jun 22, 1:46 am, Charles Sanders <C.delete_this.Sand... at BoM.GOv.AU>
wrote:
> 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

Hey Guys thanks for the feedback and the suggestions.
Charles I got your implementation to work so many thanks for this.

this is what I had so far

for spam in l1:
	for eggs in l2:
		if spam == eggs:
			print "kaka", spam, eggs

so its almost working just need the index, I'll
continue playing with the nested loop approach for a bit more.

Thanks once again guys




More information about the Python-list mailing list