faqts : Computers : Programming : Languages : Python

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

7 of 7 people (100%) answered Yes
Recently 7 of 7 people (100%) answered Yes

Entry

I need to compare two lists, but i did not find any api for that.. can you please help me out.

Aug 2nd, 2006 16:26
Reed O'Brien, sreecharan konda, http://docs.python.org/lib/set-example.html


Not sure exactly what you mean. But I'll give it a go and hope it is useful
>>> L = [1,2,3,4,5,6,7,8,9]
>>> L2 = [2,3,4,5]
>>> L3 = [7,8,9,10,11]
>>> L4 = [2,3,4,5]
>>> L2 == L4
True
>>> L2 == L 
False
>>> from sets import Set
>>> S1, S2, S3, S4 = Set(L), Set(L2), Set(L3), Set(L4)
>>> S1
Set([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> S1 & S3        # Intersections
Set([8, 9, 7])
>>> S2 | S3         # unique union 
Set([2, 3, 4, 5, 7, 8, 9, 10, 11])
>>> S1 - S2
Set([8, 1, 9, 6, 7])   #differences
>>> S1.issuperset(S2)
True
There are immutable and mutable Sets. You can modify mutable ones...
See the docs for more