【每日一题0614】找出两个list的相同元素和不同元素

#例:
a=[6,1,1,2,3]
b=[1,4,2,4,5]
'''
结果1:
    相同元素:[1,2]
    不同元素:[6,3,4,5]

结果2:
    a不同于b的元素为 [6,3] 
    b不同于a的元素为 [4,5]
'''

weishi://activity?activity_id=TxspVip6thAct&activity_normal_id=vip_rwzx_new&pid=&type=&ext_da=

a=[6,1,1,2,3]
b=[1,4,2,4,5]

c=set(a)^set(b) # 位运算非,求不同元素
d=set(a)&set(b) # 位运算与,求相同元素


端午节快乐鸭~~~


解法1:利用集合运算符

def find_same_1(inp1, inp2):
    return list(set(inp1) & set(inp2))
def find_diff_1(inp1, inp2):
    return list(set(inp1) ^ set(inp2))
assert sorted([1, 2]) == sorted(find_same_1(a, b))
assert sorted([6, 3, 4, 5]) == sorted(find_diff_1(a, b))

解法2: 利用集合的方法

def find_same_2(inp1, inp2):
    return list(set(inp1).intersection(set(inp2)))
def find_diff_2(inp1, inp2):
    return list(set(inp1).difference(set(inp2))) + list(set(inp2).difference(set(inp1)))

assert sorted([1, 2]) == sorted(find_same_2(a, b))
assert sorted([6, 3, 4, 5]) == sorted(find_diff_2(a, b))

解法3:使用推导式

def find_same_3(inp1, inp2):
    return list({x for x in inp1 for y in inp2 if y == x})

def find_diff_3(inp1, inp2):
    return list({x for x in inp1 if x not in inp2} | {y for y in inp2 if y not in inp1})

assert sorted([1, 2]) == sorted(find_same_3(a, b))
assert sorted([6, 3, 4, 5]) == sorted(find_diff_3(a, b))

解法4:利用内置函数filter

import itertools
def find_same_4(inp1, inp2):
    f1 = filter(lambda x:x in inp2, inp1)
    f2 = filter(lambda x:x in inp1, inp2)
    return list(set(f1))

def find_diff_4(inp1, inp2):
    f1 = filter(lambda x:x not in inp2, inp1)
    f2 = filter(lambda x:x not in inp1, inp2)
    return list(set(f2)) + list(set(f1))

assert sorted([1, 2]) == sorted(find_same_4(a, b))
assert sorted([6, 3, 4, 5]) == sorted(find_diff_4(a, b))

解法5:利用Counter统计

from collections import Counter

def find_same_5(inp1, inp2):
    ct = Counter(list(set(inp1)) + list(set(inp2)))
    return [k for k,v in ct.items() if v == 2]

def find_diff_5(inp1, inp2):
    ct = Counter(list(set(inp1)) + list(set(inp2)))
    return [k for k,v in ct.items() if v == 1]

assert sorted([1, 2]) == sorted(find_same_5(a, b))
assert sorted([6, 3, 4, 5]) == sorted(find_diff_5(a, b))

:partying_face: :partying_face: :partying_face: :partying_face: :partying_face:

"""
#例:
a=[6,1,1,2,3]
b=[1,4,2,4,5]
'''
结果1:
    相同元素:[1,2]
    不同元素:[6,3,4,5]

结果2:
    a不同于b的元素为 [6,3]
    b不同于a的元素为 [4,5]
'''
"""


def test(a_list: list, b_list: list):
    same = []
    different = []
    for i in a_list:
        if i in b_list and i not in same:
            same.append(i)
        elif i not in b_list and i not in different:
            different.append(i)
    for i in b_list:
        if i not in a_list and i not in different:
            different.append(i)
    return same, different


assert test([6, 1, 1, 2, 3], [1, 4, 2, 4, 5]) == ([1, 2], [6, 3, 4, 5])
assert test([], [1, 2, 3]) == ([], [1, 2, 3])
assert test([], [1, 2]) == ([], [1, 2])
# a,b交叉且相同和不同都有重复元素
assert test([5, 5, 1, 2, 4], [6, 1, 1, 2, 3, 2, 3]) == ([1, 2], [5, 4, 6, 3])

相同元素:list(set(a).intersection(set(b)))
a不同于b的元素:list(set(a).difference(set(b)))
b不同于a的元素list(set(b).difference(set(a)))
不同元素:list(set(a).difference(set(b)))+list(set(b).difference(set(a)))

1 个赞
def find_2(a:list,b:list):
    a=set(a)
    b=set(b)
    result1 = []  #相同元素
    result2 = []  #不同元素

    for i in a:     #将列表a的元素逐个与列表b对比,需处理相同的,和不同的
        if i in b:
            result1.append(i)
        else:
            result2.append(i)

    for j in b:     #将列表b的元素逐个与列表a对比,只需要处理不同的
        if j not in a:
            result2.append(j)
    if len(result1) == 0:
        return f'相同元素:无\n不同元素:{result2}'
    else:
        return f'相同元素:{result1}\n不同元素:{result2}'

print(find_2([], [1, 2, 3]))
print(find_2([11], [1, 2, 3]))
print(find_2([5, 5, 1, 2, 4], [6, 1, 1, 2, 3, 2, 3]))
def find(a:list,b:list):
    sameData = list(set(i for i in a if i in b))
    difData = list(set(a+b))

    for i in sameData:
        difData.remove(i)
    return  sameData,difData