473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Function to remove elements from a list not working

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
Jun 12 '06 #1
3 1794
Girish Sahani wrote:
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
</ot> 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 ?
getl5() 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),(1 1,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...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 12 '06 #2
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 Sahani wrote:
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
</ot>
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 ?
getl5() 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),(1 1,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...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
--
http://mail.python.org/mailman/listinfo/python-list


Jun 12 '06 #3
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.
</ot>
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.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 12 '06 #4

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

Similar topics

23
by: Stan Cook | last post by:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly...
11
by: Tony Johansson | last post by:
Hello! I have some problem with STL function remove I have two classes called Handle which is a template class and Integer which is not a template class. The Integer class is just a wrapper...
5
by: Ian Richardson | last post by:
I'm writing some code which does one thing when onreadystatechange occurs, e.g. handle.onreadystatechange = function() { blah(handle,other_params) }; ....but sometimes I need to add another,...
9
by: Max | last post by:
I'm new with Javascript and can't seem to figure out what I'm doing wrong here as I'm not able to pass a simple variable to a function. In the head of doc I have: <script...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
0
by: aredo3604gif | last post by:
I have coded a serie of singly linked lists in ANSI C which I have to use. The lists are then stored in a serie of buckets with chained hash table technique. In the various lists there are nodes...
5
by: Girish Sahani | last post by:
Hi, I am trying to modify a list of pairs (l4) by removing those pairs which are not present in a third list called pairList. The following is a simplified part of the routine i have written....
5
RMWChaos
by: RMWChaos | last post by:
I am working on a script to create and remove DOM elements, and I want to make it as efficient as possible (no redundancies). Because DOM elements each have their own set of attributes, the function...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.