啄木鸟专吃虫,故名啄木鸟。
全部博文(67)
分类: Python/Ruby
2014-05-23 10:05:20
>>> a = ['i','am','a','big','apple']
>>> b = ['you','are','a','apple']
>>> print list(set(a).intersection(set(b)))#两个list求交集
['a', 'apple']
>>> print [s for s in b if s in a]#两个list求交集方法2
['a', 'apple']
>>> print list(set(a).union(set(b)))#两个list求并集
['a', 'apple', 'i', 'big', 'am', 'are', 'you']
>>> for i in b:#两个list求并集2
a.append(i)
>>> print a
['i', 'am', 'a', 'big', 'apple', 'you', 'are', 'a', 'apple']
>>> print list(set(b).difference(set(a)))#两个list求差集,b中有a中没有
['you', 'are']
>>> print [s for s in b if s not in a]#两个list求差集2
['you', 'are']