【每日一题1018】列表对比

:mage:t2: 给定两个数字列表a 和 b,请编写一个函数list_diff(a, b),将列表a中的同时也在b列表中出现的元素移除,并且保留原有的位置顺序。函数返回对比后的列表。
示例:
输入:a=[1,2], b=[1],输出:[2]
输入:a=[1,2,2,2,3], b=[2],输出:[1, 3]
题目难度:中等
题目来源:codewars: Array.diff

def list_diff(a: list, b: list) ->list:
    pass

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:
    diff_list = []
    for i in range(len(a)):
        if a[i] not in b:
            diff_list.append(a[i])
    return diff_list


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:
    offset = 0
    for i in range(len(a)):
        if a[i - offset] in b:
            del a[i - offset]
            offset += 1
    return 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]
def list_diff(a: list, b: list) -> list:
    return [n for n in a if n 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(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:
    nums = list(set(a) & set(b))
    for i in nums:
        a.remove(i)
    return a


list_diff([1, 2], [1]) == [2]
list_diff([1, 2, 2], [1]) == [2, 2]
list_diff([1, 2, 2, 2, 3], [2]) == [1, 3]
list_diff([1, 2, 3, 4, 3, 2], [2, 3]) == [1, 4]
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:
    result = []
    for i in a:
        if i not in b:
            result.append(i)
    return result

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:
    for el in b:
        while el in a:
            a.remove(el)
    return a
def list_diff(a: list, b: list) -> list:
    list1 = []
    for i in a:
        if i not in b:
            list1.append(i)
    return list1


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:
    for x in a:
        if x in b:
            a.remove(x)
    return a


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 [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]