def list_diff(list1: List[int], list2: List[int]) -> List[int]:
# 计算两个列表的交集
list_intersection = list(set(list1) & set(list2))
# 循环交集,去除list1有交集的值
for i in list_intersection:
while i in list1:
list1.remove(i)
return list1
def list_diff(a: list, b: list) ->list:
# 将列表a转化为集合
set_a = set(a)
# 将列表b转化为集合
set_b = set(b)
# 进行a集合与b集合的差集运算
result = set_a.difference(set_b)
# 由于转成集合会导致去除重复元素
# 需要在列表a中遍历出差集中的所有元素,返回列表
result = [v for v in a if v in result ]
return result
assert list_diff([1,2], [1]) == [2]
assert list_diff([1,2,2], [1]) == [2,2]
assert list_diff([1,2,2,2,3], [2]) == [1,3]
def list_diff(a: list, b: list) -> list:
list = []
for i in a:
if i not in b:
list.append(i)
return list
list_diff([1,2], [1]) == [2]
list_diff([1,2,2], [1]) == [2,2]
list_diff([1,2,2,2,3], [2]) == [1,3]
def list_diff(a: list, b: list) ->list:
return [item for item in a if item not in b ]
assert list_diff([1,2], [1]) == [2]
assert list_diff([1,2,2], [1]) == [2,2]
assert list_diff([1,2,2,2,3], [2]) == [1,3]
def list_diff(a: list, b: list) ->list:
return [i for i in a if i not in b] + [j for j in b if j not in a]
assert list_diff([1,2], [1]) == [2]
assert list_diff([1,2,2], [1]) == [2,2]
assert list_diff([1,2,2,2,3], [2]) == [1,3]