Friday, April 1, 2011

[Level 2] How to compare two list (array) in Python

#!/bin/env python
a=[1,2,3]
b=[2,3,4]
set_a = set(a)
set_b = set(b)
print set_a.difference(set_b)
print set_b.difference(set_a)
print set_a.union(set_b)
print set_a.intersection(set_b)
# print set_a.difference(set_b)[0] ## fail, 'set' object does not support indexing
print list(set_a.difference(set_b))[0]
$ ./t.py
set([1])
set([4])
set([1, 2, 3, 4])
set([2, 3])
1

Wish this helps. regards, Stanley Huang