comparing two lists and returning "position"

hiro NunezD at gmail.com
Fri Jun 22 13:40:10 EDT 2007


On Jun 22, 2:16 am, hiro <Nun... at gmail.com> wrote:
> 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

Hi once again, Charles.. I have tried your approach in my data set l2
and it keeps crashing on me,
bare in mind that I have a little over 10 million objects in my list
(l2) and l1 contains around 4 thousand
objects.. (i have enough ram in my computer so memory is not a
problem)

python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32

error is : ValueError: list.index(x): x not in list

when using Charles's
pos = [ l2.index(i) for i in l1 ]
print pos

does anybody know of if I have to many data points ? the nested for
loop approach seems to be working(still have get the index "position"
returned though)
Charles's approach works fine with less data.

Cheers, -d




More information about the Python-list mailing list