Faqts : Computers : Programming : Languages : Python : Snippets : Lists

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

24 of 26 people (92%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Efficient list subtraction

Sep 15th, 2009 02:20
jick heir, free musicpoint, engatoo engatoo, osososo didididi, Nathan Wallace, unknown unknown, Hans Nowak, Snippet 95, Steven D. Majewski


"""
Packages: basic_datatypes.lists;functional_programming
"""
""" 
> Does anyone out there have a simple way to compare two lists (some 
operator 
> perhaps) and return
> a list of *differences*?  By differences, I mean the following:  If 
had two
> lists  like,
> 
> a = [1, 4, 5, 100]
> b = [4, 5]
> 
> the difference between a and b (a-b) would be [1, 100]. I suppose 
you could
> write a couple of for loops to go through each list and compare 
them, but I
> thought there might be a simple way to do this.
> 
> I'm also interested in a simple way to returning a list of what is 
identical 
> between the two lists.
> In the case of the above lists, a and b, they both contain [4, 5].
> 
Well -- it's probably not the most efficient, but the simplest 
list intersection  is probably:
"""
a = [1,4,5,100]
b = [4,5]
print filter( lambda x: x in a, b )
# [4, 5]
print filter( lambda x: x in b, a )     # order doesn't matter
# [4, 5]                                # for intersection
                                        # but it does for set-
difference
print filter( lambda x: x not in a, b )       # b - a 
# []
print filter( lambda x: x not in b, a )       # a - b 
# [1, 100]
"""
I don't think union or XOR can be done as concisely. 
"""
http://sundhedernaeringguide.blogspot.com/
http://zdravevyzivypruvodce.blogspot.com/
http://guidasalutenutrizione.blogspot.com/
http://gesundheiternahrungpflege.blogspot.com/
http://soinssantenutrition.blogspot.com/
http://zdravljeprehranavodic.blogspot.com/
http://healthnutritionhoito.blogspot.com/
http://hlhnutgr.blogspot.com/
http://gezondheidvoedinggids.blogspot.com/
http://taplalkozas-egeszsegugyikezikonyv.blogspot.com/
http://hlthnutbg.blogspot.com/
------------------------------------------------------------------
http://dl3.com/link/category/voice
http://dl3.com/link/category/chat
http://dl3.com/link/category/song
http://dl3.com/link
http://www.dl3.com/smiles
http://www.dl3.com/cards
http://games.dl3.com
http://www.chataluae.com
http://www.dl3.com/vb/index.php
http://www.dl3.com/vb
http://www.dl3.com/vb
http://www.dl3.com
http://www.stop10.com
http://topics.ll6.com
http://topics.ll6.com
http://www.stop10.com
http://www.ll6.com/java.shtml
http://dlel.ll6.com/c21.html
http://dlel.ll6.com/c20.html
http://dlel.ll6.com/c19.html
http://dlel.ll6.com/c18.html
http://dlel.ll6.com/c17.html
http://dlel.ll6.com/categories.xml
http://dlel.ll6.com/sitemap.xml
http://dlel.ll6.com/c10.html
http://dlel.ll6.com/c9.html
http://dlel.ll6.com/c8.html
http://dlel.ll6.com/c7.html
http://dlel.ll6.com/c6.html
http://dlel.ll6.com/c5.html
http://dlel.ll6.com/c4.html
http://dlel.ll6.com/c3.html
http://dlel.ll6.com/c2.html
http://dlel.ll6.com/c10.html
http://dlel.ll6.com/c16.html
http://dlel.ll6.com/c15.html
http://dlel.ll6.com/c14.html
http://dlel.ll6.com/c13.html
http://dlel.ll6.com/c12.html
http://dlel.ll6.com/c11.html
http://voice.ll6.com
http://www.ll6.com
http://vb.ll6.com/forumdisplay.php?f=50
http://vb.ll6.com/forumdisplay.php?f=41
http://vb.ll6.com/forumdisplay.php?f=35
http://vb.ll6.com/forumdisplay.php?f=34
http://vb.ll6.com/forumdisplay.php?f=33
http://vb.ll6.com/forumdisplay.php?f=32
http://vb.ll6.com/forumdisplay.php?f=31
http://vb.ll6.com/forumdisplay.php?f=30
http://vb.ll6.com/forumdisplay.php?f=29
http://vb.ll6.com/forumdisplay.php?f=28
http://vb.ll6.com/forumdisplay.php?f=27
http://vb.ll6.com/forumdisplay.php?f=26
http://vb.ll6.com/forumdisplay.php?f=25
http://vb.ll6.com/forumdisplay.php?f=24
http://vb.ll6.com/forumdisplay.php?f=23
http://vb.ll6.com/forumdisplay.php?f=22
http://vb.ll6.com/forumdisplay.php?f=21
http://vb.ll6.com/forumdisplay.php?f=20
http://vb.ll6.com/forumdisplay.php?f=19
http://vb.ll6.com/forumdisplay.php?f=18
http://vb.ll6.com/forumdisplay.php?f=17
http://vb.ll6.com/forumdisplay.php?f=5
http://vb.ll6.com/forumdisplay.php?f=6
http://vb.ll6.com/forumdisplay.php?f=10
http://vb.ll6.com/forumdisplay.php?f=4
http://vb.ll6.com/index.php
http://ll6.com
http://www.alshalah.com
http://www.alshalah.com/chat1420.html
http://www.alshalah.com/music22.html
http://www.alshalah.com/music21.html
http://www.alshalah.com/chat1355.html
http://www.alshalah.com/chat1507.html
http://www.alshalah.com/download7.html
http://www.alshalah.com/download7.html
http://www.alshalah.com/chat1337.html
http://www.alshalah.com/chat100.html
http://www.alshalah.com/music12.html
http://www.alshalah.com/music12.html
http://www.alshalah.com/download5.html
http://www.alshalah.com/music23.html
http://www.alshalah.com/chat1339.html
http://www.alshalah.com/chat103.html
http://www.alshalah.com/music15.html
http://www.alshalah.com/chat1528.html
http://www.alshalah.com/chat1359.html
http://www.alshalah.com/chat1519.html
http://www.alshalah.com/chat1422.html
http://www.alshalah.com/music4.html
http://www.alshalah.com/chat101.html
http://www.alshalah.com/chat106.html
http://www.alshalah.com/chat102.html
http://www.alshalah.com/chat1166.html
http://www.alshalah.com/vote.php
http://www.alshalah.com/visit.php
http://www.alshalah.com/chat2821.html
http://www.alshalah.com/chat2821.html
http://www.alshalah.com/chat2821.html
http://www.alshalah.com/chat2821.html
http://www.alshalah.com/chat2820.html
http://www.alshalah.com/chat2820.html
http://www.alshalah.com/chat2819.html
http://www.alshalah.com/chat2819.html
http://www.alshalah.com/chat2818.html
http://www.alshalah.com/chat2818.html
http://www.alshalah.com/chat2817.html
http://www.alshalah.com/chat2817.html
http://www.alshalah.com/chat2816.html
http://www.alshalah.com/chat2816.html
http://www.alshalah.com/chat2815.html
http://www.alshalah.com/chat2815.html
http://www.alshalah.com/chat2814.html
http://www.alshalah.com/chat2814.html
http://www.alshalah.com/chat2813.html
http://www.alshalah.com/chat2813.html
http://onlineseosolution.com
http://seosolutiononline.blogspot.com/	   
http://logicdunia.com
http://logicdunia.blogspot.com/	   
http://just2world.com
http://just2world.blogspot.com/   	   
http://promotioncrown.com	   
http://casinogametown.com
http://promotioncrown.blogspot.com/
http://casinogametown.blogspot.com/	   
http://indiaeduresults.com
http://indiaeduresults.blogspot.com/	   
http://http://realmaxor.com
http://realmaxor.blogspot.com/	   
http://a1start.com
http://a1start.blogspot.com/
http://alwarmall.com	   
http://terrymart.com
http://terrymarts.blogspot.com/	   
http://rajasnet.com
http://rajasnets.blogspot.com/	   
http://khairthal.com
http://khairthal.blogspot.com/	   
http://jestrugs.com
http://jestrugs.blogspot.com/	   
http://furnimpact.com
http://furnimpacts.blogspot.com/	   
http://srplanet.com
http://srplanets.blogspot.com/	   
http://greatspido.com
http://greatspidos.blogspot.com/	   
http://habibstyle.com
http://habibstyle.blogspot.com/	   
http://clarktimes.com
http://clarktime.blogspot.com/	   
http://b2bgrant.com
http://b2bgrant.blogspot.com/	   
http://propertybunch.com
http://propertybunch.blogspot.com/	   
http://buxorgroup.com
http://buxorgroup.blogspot.com/	   
http://gsmrainbow.com
http://gsmrainbow.blogspot.com/	   
http://lgfsolution.com
http://lgfsolution.blogspot.com/	   
http://nightroutes.com
http://nightroutes.blogspot.com/	   
http://monilek.com
http://monileks.blogspot.com/   
http://pearlaction.com
http://pearlaction.blogspot.com/	   
http://WebsZone.com	   
http://sariskas.com
http://sariskas.blogspot.com/
http://www.alshalah.com/vb/picturet1067.html
http://www.stop10.com
http://www.alshalah.com/chat27.html
http://www.alshalah.com/vb/picturet10871.html
http://www.alshalah.com/vb/picturet46798.html
http://www.alshalah.com/vb/picturet4995.html
http://www.alshalah.com/vb/picturet4445.html
http://www.alshalah.com/vb/picturet17029.html
http://www.alshalah.com/chat2794.html
http://www.alshalah.com/vb/picturet42026.html
http://www.alshalah.com/vb/picturet28063.html
http://www.alshalah.com/vb/picturet27011.html
http://www.alshalah.com/vb/picturet3521.html
http://www.alshalah.com/vb/picturet25360.html
http://www.alshalah.com/chat2793.html
http://www.alshalah.com/chat886.html
http://www.alshalah.com/vb/picturet274.htmll
http://www.alshalah.com/vb/picturet274.html
http://www.alshalah.com/music16.html
http://www.alshalah.com/download7.html
http://www.alshalah.com/vb/picturet43943.html
http://www.alshalah.com/chat2666.html
http://www.alshalah.com/music28.html
http://www.alshalah.com/music28.html
http://www.alshalah.com/music15.html
http://www.alshalah.com/music28.html
http://www.roo7e.com
http://www.roo7e.com
http://www.al33ab.com
http://ksacam.com
http://chat.ksacam.com
http://ksacam.info
http://oo2o.com
http://s4cam.com
http://saudi4cam.com				
http://voice.ksacam.com
http://سعودي-كام.oo2o.com
http://سعوديكام.com
http://xn--mgbply5cnrr.com
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/
http://www.dir.3rabstarz.com/
http://www.up.3rabstarz.com
http://www.3rabstarz.com/vb/forumdisplay.php?f=5
http://www.3rabstarz.com/vb/forumdisplay.php?f=116
http://www.3rabstarz.com/vb/forumdisplay.php?f=66
http://www.3rabstarz.com/vb/forumdisplay.php?f=78
http://www.3rabstarz.com/vb/forumdisplay.php?f=147
http://www.3rabstarz.com/vb/forumdisplay.php?f=2
http://www.3rabstarz.com/vb/forumdisplay.php?f=31
http://www.3rabstarz.com/vb/forumdisplay.php?f=136
http://www.3rabstarz.com/vb/forumdisplay.php?f=34
http://www.3rabstarz.com/vb/forumdisplay.php?f=89
http://www.3rabstarz.com/vb/forumdisplay.php?f=26
http://www.3rabstarz.com/vb/forumdisplay.php?f=72
http://www.3rabstarz.com/vb/forumdisplay.php?f=73
http://www.3rabstarz.com/vb/forumdisplay.php?f=149
http://www.3rabstarz.com/vb/forumdisplay.php?f=150
http://www.3rabstarz.com/vb/forumdisplay.php?f=151
http://www.3rabstarz.com/vb/forumdisplay.php?f=152
http://www.3rabstarz.com/vb/forumdisplay.php?f=67
http://www.3rabstarz.com/vb/forumdisplay.php?f=3
http://www.3rabstarz.com/vb/forumdisplay.php?f=37
http://www.3rabstarz.com/vb/forumdisplay.php?f=115
http://www.3rabstarz.com/vb/forumdisplay.php?f=76
http://www.3rabstarz.com/vb/forumdisplay.php?f=77
http://www.3rabstarz.com/vb/forumdisplay.php?f=36
http://www.3rabstarz.com/vb/forumdisplay.php?f=153
http://www.3rabstarz.com/vb/forumdisplay.php?f=122
http://www.3rabstarz.com/vb/forumdisplay.php?f=118
http://www.3rabstarz.com/vb/forumdisplay.php?f=119
http://www.3rabstarz.com/vb/forumdisplay.php?f=120
http://www.3rabstarz.com/vb/forumdisplay.php?f=121
http://www.3rabstarz.com/vb/forumdisplay.php?f=35
http://www.3rabstarz.com/vb/forumdisplay.php?f=65
http://www.3rabstarz.com/vb/forumdisplay.php?f=7
http://www.3rabstarz.com/vb/forumdisplay.php?f=9
http://www.3rabstarz.com/vb/forumdisplay.php?f=11
http://www.3rabstarz.com/vb/forumdisplay.php?f=142
http://www.3rabstarz.com/vb/forumdisplay.php?f=143
http://www.3rabstarz.com/vb/forumdisplay.php?f=146
http://www.3rabstarz.com/vb/forumdisplay.php?f=140
http://www.3rabstarz.com/vb/forumdisplay.php?f=137
http://www.3rabstarz.com/vb/forumdisplay.php?f=114
http://www.3rabstarz.com/vb/forumdisplay.php?f=117
http://www.3rabstarz.com/vb/forumdisplay.php?f=113
http://www.3rabstarz.com/vb/forumdisplay.php?f=51
http://www.3rabstarz.com/vb/forumdisplay.php?f=84
http://www.3rabstarz.com/vb/forumdisplay.php?f=138
http://www.3rabstarz.com/vb/forumdisplay.php?f=139
http://www.3rabstarz.com/vb/forumdisplay.php?f=15
http://www.3rabstarz.com/vb/forumdisplay.php?f=130
http://www.3rabstarz.com/vb/forumdisplay.php?f=97
http://www.3rabstarz.com/vb/forumdisplay.php?f=102
http://www.3rabstarz.com/vb/forumdisplay.php?f=101
http://www.3rabstarz.com/vb/forumdisplay.php?f=104
http://www.3rabstarz.com/vb/forumdisplay.php?f=131
http://www.3rabstarz.com/vb/forumdisplay.php?f=103
http://www.3rabstarz.com/vb/forumdisplay.php?f=99
http://www.3rabstarz.com/vb/forumdisplay.php?f=59
http://www.3rabstarz.com/vb/forumdisplay.php?f=60
http://www.3rabstarz.com/vb/forumdisplay.php?f=39
http://www.3rabstarz.com/vb/forumdisplay.php?f=40
http://www.3rabstarz.com/vb/forumdisplay.php?f=42
http://www.3rabstarz.com/vb/forumdisplay.php?f=4
http://www.3rabstarz.com/vb/forumdisplay.php?f=1
http://www.3rabstarz.com/vb/forumdisplay.php?f=88
http://www.3rabstarz.com/vb/forumdisplay.php?f=148
http://www.3rabstarz.com/vb/forumdisplay.php?f=62
http://www.3rabstarz.com/vb/forumdisplay.php?f=63
http://www.3rabstarz.com/vb/forumdisplay.php?f=64
http://www.3rabstarz.com/vb/forumdisplay.php?f=6
http://www.3rabstarz.com/vb/forumdisplay.php?f=12
http://www.3rabstarz.com/vb/forumdisplay.php?f=96
http://www.3rabstarz.com/vb/forumdisplay.php?f=58
http://www.3rabstarz.com/vb/forumdisplay.php?f=38