Function to remove elements from a list not working

G

Girish Sahani

Hi,
I am trying to convert a list of pairs (l4) to list l5 by removing those
pairs from l4 which are not present in a third list called pairList.
The following is a simplified part of the routine i have written. However
it does not give the correct output. Please help!
Its possible i have made a trivial mistke since i am a newbie.

def getl5():
l5 = []
pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]]
l4 = [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]]
for pair in l4:
if pair not in pairList:
element.remove(l4)
l5.append(element)
print "l5 is",l5
 
B

bruno at modulix

Girish said:
Hi,
I am trying to convert a list of pairs (l4) to list l5 by removing those
pairs from l4 which are not present in a third list called pairList.



The following is a simplified part of the routine i have written. However
it does not give the correct output. Please help!
Its possible i have made a trivial mistke since i am a newbie.

def getl5():
<ot>
Please avoid this kind of names : it's both meaningless (what the ... is
'l5', I wonder ???) and confusing (is it 'l5' or '15' ?)

FWIW, read this:
http://mindprod.com/jgloss/unmain.html
l5 = []
pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]]

From a semantic POV, you should use tuples for pairs - not lists.
l4 = [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]]
for pair in l4:
if pair not in pairList: err... see below...
element.remove(l4)
l5.append(element)
This is outside the for loop, so this would be executed only once (if
the rest of the code was correct, of course...)
print "l5 is",l5

You did not test this code, did you ?
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/tmp/python-961l_S.py", line 7, in getl5
NameError: global name 'element' is not defined
The SimpleStupid(tm) way to do this is far more simple - at least if I
understood your specifications:

pairList = [(1,2),(3,4),(3,5),(3,6),(9,7),(8,9),(8,7),(7,9),(11,10)]
l4 = [(4,2),(4,7),(4,10),(4,12),(9,2),(9,7),(9,10),(9,12),(11,2),(11,7)]
l5 = [pair for pair in l4 if pair in pairList]
print "l5 is : ", l5

Now this is not necessarily the most efficient solution...
 
G

Girish Sahani

Hey Bruno...you are seeing the wrong post :p...please ignore this and
check out the one with (corrected) appended at the end...
Also, i used the list comprehension thingy which u have given, but now the
problem is python just hangs if my list l4 contains around 50
pairs...considering its not that big a data, this shouldnt happen....
Should it??

Girish said:
Hi,
I am trying to convert a list of pairs (l4) to list l5 by removing
those
pairs from l4 which are not present in a third list called pairList.



The following is a simplified part of the routine i have written.
However
it does not give the correct output. Please help!
Its possible i have made a trivial mistke since i am a newbie.

def getl5():
<ot>
Please avoid this kind of names : it's both meaningless (what the ... is
'l5', I wonder ???) and confusing (is it 'l5' or '15' ?)

FWIW, read this:
http://mindprod.com/jgloss/unmain.html
l5 = []
pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]]
From a semantic POV, you should use tuples for pairs - not lists.
l4 =
[[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]]
for pair in l4:
if pair not in pairList: err... see below...
element.remove(l4)
l5.append(element)
This is outside the for loop, so this would be executed only once (if
the rest of the code was correct, of course...)
print "l5 is",l5

You did not test this code, did you ?
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/tmp/python-961l_S.py", line 7, in getl5
NameError: global name 'element' is not defined
The SimpleStupid(tm) way to do this is far more simple - at least if I
understood your specifications:

pairList = [(1,2),(3,4),(3,5),(3,6),(9,7),(8,9),(8,7),(7,9),(11,10)]
l4 = [(4,2),(4,7),(4,10),(4,12),(9,2),(9,7),(9,10),(9,12),(11,2),(11,7)]
l5 = [pair for pair in l4 if pair in pairList]
print "l5 is : ", l5

Now this is not necessarily the most efficient solution...
 
B

bruno at modulix

Girish Sahani wrote:
(please don't top-post)
Hey Bruno...you are seeing the wrong post :p...please ignore this and
check out the one with (corrected) appended at the end...

<ot>
You should have posted the correction in the same thread.
Also, i used the list comprehension thingy which u have given, but now the
problem is python just hangs if my list l4 contains around 50
pairs...considering its not that big a data, this shouldnt happen....
Should it??

It shouldn't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top