Chinaunix首页 | 论坛 | 博客
  • 博客访问: 104513
  • 博文数量: 67
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 577
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-16 09:51
个人简介

啄木鸟专吃虫,故名啄木鸟。

分类: 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']

阅读(666) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~