Pyton 获取两个 list 的差集、并集

来源:互联网 发布:2016淘宝热卖行业 编辑:程序博客网 时间:2024/06/05 08:17
求差:
  1. method one:
    list_one = [1, 2, 3]listd_two = [2, 3]diff = []for  x in list_one:    if x not in list_two:        diff.append(x)

  2. method two:
    diff = [x for x in list_one if x not in list_two]

  3. method three:
    diff = list(set(list_one)^set(list_two))

  4. method four:
    diff = list(set(list_one).difference(set(list_two))) one中有而two中没有的

求和:
tt = list(set(list_one).union(set(list_two)))

求交:
for x in list_one:    if x in list_two:        jj.append(x)