472,958 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

comparing two lists and returning "position"

Hi there, I have a 2 lists.. for simplicities sake lets say the are:

l1 = [ 'abc' 'ghi' 'mno' ]

l2 = [ 'abc' 'def' 'ghi' 'jkl 'mno' 'pqr']

what I need to do is compare l1 against l2 and return the "position"
of where each object in l1 is in l2

ie: pos = 0, 2, 4
Thanks in advance, -h

Jun 22 '07 #1
12 5807
On 2007-06-22, hiro <Nu****@gmail.comwrote:
Hi there, I have a 2 lists.. for simplicities sake lets say the are:

l1 = [ 'abc' 'ghi' 'mno' ]

l2 = [ 'abc' 'def' 'ghi' 'jkl 'mno' 'pqr']

what I need to do is compare l1 against l2 and return the "position"
of where each object in l1 is in l2

ie: pos = 0, 2, 4

Thanks in advance, -h
Come, come! You can try harder than that.

--
Neil Cerutti
Jun 22 '07 #2
hiro <Nu****@gmail.comwrites:
what I need to do is compare l1 against l2 and return the "position"
of where each object in l1 is in l2

ie: pos = 0, 2, 4
Is it September already?

from itertools import izip
pos = map(dict(izip(l2, count())).__getitem__, l1)

Heh heh heh.
Jun 22 '07 #3
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
Jun 22 '07 #4
On Jun 22, 1:46 am, Charles Sanders <C.delete_this.Sand...@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

Jun 22 '07 #5
On Fri, 22 Jun 2007 03:11:16 +0000, hiro wrote:
Hi there, I have a 2 lists.. for simplicities sake lets say the are:

l1 = [ 'abc' 'ghi' 'mno' ]

l2 = [ 'abc' 'def' 'ghi' 'jkl 'mno' 'pqr']

what I need to do is compare l1 against l2 and return the "position" of
where each object in l1 is in l2

ie: pos = 0, 2, 4

Thanks for sharing. Did you have a question, or did you just want to tell
us what you were doing?
Thanks in advance, -h
My pleasure.
--
Steven.
Jun 22 '07 #6
On Jun 22, 2:16 am, hiro <Nun...@gmail.comwrote:
On Jun 22, 1:46 am, Charles Sanders <C.delete_this.Sand...@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

Jun 22 '07 #7
In <11**********************@u2g2000hsc.googlegroups. com>, hiro wrote:
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
So you are saying you get this error with the value of `x` actually in the
list!? Somehow hard to believe.

Ciao,
Marc 'BlackJack' Rintsch
Jun 22 '07 #8
On Jun 22, 1:56 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
In <1182534010.884992.119...@u2g2000hsc.googlegroups. com>, hiro wrote:
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

So you are saying you get this error with the value of `x` actually in the
list!? Somehow hard to believe.

Ciao,
Marc 'BlackJack' Rintsch
yes I do

Jun 22 '07 #9
On Jun 22, 1:58 pm, hiro <Nun...@gmail.comwrote:
On Jun 22, 1:56 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
In <1182534010.884992.119...@u2g2000hsc.googlegroups. com>, hiro wrote:
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
So you are saying you get this error with the value of `x` actually in the
list!? Somehow hard to believe.
Ciao,
Marc 'BlackJack' Rintsch

yes I do
I doubled, trippled check my data already (even doing a search by hand
using vim) and the data is fine. Still looking into it though

Jun 22 '07 #10
On Jun 22, 2:00 pm, hiro <Nun...@gmail.comwrote:
On Jun 22, 1:58 pm, hiro <Nun...@gmail.comwrote:
On Jun 22, 1:56 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
In <1182534010.884992.119...@u2g2000hsc.googlegroups. com>, hiro wrote:
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
So you are saying you get this error with the value of `x` actually in the
list!? Somehow hard to believe.
Ciao,
Marc 'BlackJack' Rintsch
yes I do

I doubled, trippled check my data already (even doing a search by hand
using vim) and the data is fine. Still looking into it though

hahaha, K found out what was wrong.. in the function computing
the data for l1 there was extra space was being put in.

ie:

l1 = [ 'abc ' 'ghi ' 'mno ' ]

and I didn't strip it properly after splitting it.. silly me,

well.. live and learn.. thanks guys

Cheers, -h

Jun 22 '07 #11
hiro wrote:
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)
Glad to see you solved the problem with the trailing space.

Just one minor point, I did say
or probably less efficiently ...
As far as i know, my suggestion's running time is
proportional to len(l1)*len(l2), which gets quite
big for your case where l1 and l2 are large lists.

If I understand how python dictionaries work, Paul Rubin's
suggestion
from itertools import izip, count
pos = map(dict(izip(l2, count())).__getitem__, l1)
or the (I think) approximately equivalent

from itertools import izip, count
d = dict(izip(l2,count()))
pos = [ d[i] for i in l1 ]

or the more memory intensive

d = dict(zip(l2,range(len(l2))))
pos = [ d[i] for i in l1 ]

should all take take running time proportional to
(len(l1)+len(l2))*log(len(l2))

For len(l1)=4,000 and len(l2)=10,000,000
Paul's suggestion is likely to take
about 1/100th of the time to run, ie
be about 100 times as fast. I was trying
to point out a somewhat clearer and simpler
(but slower) alternative.

Charles
Jun 25 '07 #12
Charles Sanders <C.*******************@BoM.GOv.AUwrites:
from itertools import izip, count
d = dict(izip(l2,count()))
pos = [ d[i] for i in l1 ]

or the more memory intensive

d = dict(zip(l2,range(len(l2))))
pos = [ d[i] for i in l1 ]
If you're itertools-phobic you could alternatively write

d = dict((x,i) for i,x in enumerate(l2))
pos = [ d[i] for i in l1 ]

dict access and update is supposed to take approximately constant time,
btw. They are implemented as hash tables.
Jun 25 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark Bordner | last post by:
Hi All, Given the following XML, I've been trying to figure out what XPATH expression will give me the "position" of a <lineitem> within the <shipto> that is its grandparent. To give you an...
1
by: nobody | last post by:
hi there! given <!ELEMENT a (#PCDATA | x)*> <!ELEMENT x (#PCDATA)> how can I find out if x is "embedded" at the beginning <a><x>xxx</x>aaa</a> or at the end <a>aaa<x>xxx</x></a> or in the...
0
by: Luigi | last post by:
I would know what "criteria" is used in box positioning with the position property. Here my tries (using IE and Opera): I have a box with an absolute positioning (properties used: position,...
2
by: Jessard | last post by:
Hi All, I am writing a .NET app that accesses an access View. The view has a table column called 'Position'. When i try and right an UpdateCommand string for this using this column I am...
4
by: Ghyslaine Crespe | last post by:
Hello, In my script, the line document.getElementById(id).style.background-position = "-200px -500px"; fails ! So, how can I change the background-position value ?
3
by: Giovanni Bassi | last post by:
Hello All, I have a class implementing IList and a global object of this class type. I have two different form objects. One has a ListBox and the other has ComboBox. Both of their datasources...
1
by: tschoepi | last post by:
Hi there, if I try to access a table named Position in an Access-Database via OleDbDataAdapter I get an error. If I rename the table to e.g. Positions it works. Any idea how to solve this...
2
by: petermichaux | last post by:
Hi, I tried the following and everything worked fine. element.style.position="relative"; Then I tried to make the CSS rule important and it didn't work. The positioning was all wrong in...
1
by: vunet.us | last post by:
Mozilla reported the fix to this bug: https://bugzilla.mozilla.org/show_bug.cgi?id=167801. When input text field is located over div, the cursor cannot be seen unless special CSS properties are...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.