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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list not working
Le Lundi 12 Juin 2006 10:12, Girish Sahani a écrit : 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) element ??? print l5 is,l5 -- _ Maric Michaud _ Aristote - www.aristote.info 3 place des tapis 69004 Lyon Tel: +33 426 880 097 -- http://mail.python.org/mailman/listinfo/python-list
Function to remove elements from a list not working (corrected)
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. 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: l4.remove(pair) print l4 is,l4 The output given is: l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]] -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list not working (corrected)
Le Lundi 12 Juin 2006 10:25, Girish Sahani a écrit : 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. 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: l4.remove(pair) print l4 is,l4 You are trying to modify a list while iterating over it, never do that ! The output given is: l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]] Is this work in your case ? def getl5(): 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]] l4 = [ e for e in l4 if e in pairList ] print l5 is, l4 -- _ Maric Michaud _ Aristote - www.aristote.info 3 place des tapis 69004 Lyon Tel: +33 426 880 097 -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list not working (corrected)
Girish Sahani wrote: 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. 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: l4.remove(pair) print l4 is,l4 The output given is: l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]] It is better to iterate over a copy, e.g. like this: 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: l4.remove(pair) print l4 is,l4 -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list not working (corrected)
Thanks!It workedi wasted a lot of time trying to find a bug in my code...what is wrong in iterating over a list and modifying it? Doesnt python take the modified list every time the loop starts? Also in this case, i want to add a condition that if none of the pairs are in pairList, element = []. How can i do that? Le Lundi 12 Juin 2006 10:25, Girish Sahani a écrit : 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. 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: l4.remove(pair) print l4 is,l4 You are trying to modify a list while iterating over it, never do that ! The output given is: l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]] Is this work in your case ? def getl5(): 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]] l4 = [ e for e in l4 if e in pairList ] print l5 is, l4 -- _ Maric Michaud _ Aristote - www.aristote.info 3 place des tapis 69004 Lyon Tel: +33 426 880 097 -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list not working
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),(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... -- bruno desthuilliers python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')]) -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list not working (corrected)
Thank you Markthis works too... Btw going slightly off-topic, when i try to run a code like below with around 50 elements (pairs) in l4,python just hangs. Any ideas why this is happening...the data is not that large :(( Girish Sahani wrote: 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. 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: l4.remove(pair) print l4 is,l4 The output given is: l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]] It is better to iterate over a copy, e.g. like this: 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: l4.remove(pair) print l4 is,l4 -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list not working (corrected)
Girish Sahani wrote: 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. 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: l4.remove(pair) print l4 is,l4 The output given is: l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]] use sets def gets5() : pairSet = set([(1,2),(3,4),(3,5),(3,6),(9,7),(8,9),(8,7),(7,9),(11,10)]) s4= set([(4,2),(4,7),(4,10),(4,12),(9,2),(9,7),(9,10),(9,12),(11,2),(11,7)]) s4 = pairSet print s4 is,s4 the output is s4 is set([(9, 7)]) -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list not working (corrected)
Girish Sahani [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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. However it does not give the correct output. Please help! Its possible i have made a trivial mistke since i am a newbie. You've fallen victim to one of the Classic Blunders! The First is Never start a land war in Asia!, but the second, only slightly lesser known is Never modify a list that you are iterating over! -- Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list not working (corrected)
Girish Sahani wrote: Btw going slightly off-topic, when i try to run a code like below with around 50 elements (pairs) in l4,python just hangs. Any ideas why this is happening...the data is not that large :(( building a filtered new list by repeatedly removing stuff from a copy of the original list isn't exactly the fastest way to do things, but there's no way the following code will hang with 50 items instead of 10, unless your computer is ludicrously slow (it takes about 0.000113 seconds on my machine). maybe you meant to write 50k items ? (11 seconds on my machine) 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: l4.remove(pair) print l4 is,l4 /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Function to remove elements from a list not working (corrected)
Girish Sahani wrote: Thanks!It workedi wasted a lot of time trying to find a bug in my code...what is wrong in iterating over a list and modifying it? Doesnt python take the modified list every time the loop starts? this is explained in the tutorial, under the for statement: The for loop maintains an internal loop variable, and you may get unexpected results if you try to modify the sequence being iterated over in the loop (this can only happen for mutable sequence types, such as lists). To safely modify the list you are iterating over (for example, to duplicate selected items), you must iterate over a copy. The slice notation makes this particularly convenient: for x in a[:]: # make a slice copy of the entire list ...if len(x) 6: a.insert(0, x) ... a ['defenestrate', 'cat', 'window', 'defenestrate'] (see http://pytut.infogami.com/node6.html ) /F -- http://mail.python.org/mailman/listinfo/python-list