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

求差:
  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)

你可能感兴趣的:(Pyton 获取两个 list 的差集、并集)