将两个列表合并为一个列表并丢弃重复项。 Pandas Python
Merge two lists in to one list and discard the duplicates. Pandas Python
想要合并两个列表并丢弃相交的元素
A = ['a', 'b', 'c', 'd']
B = ['a', 'b', 'd', 'e', 'f']
预期结果:
['c', 'e', 'f']
我可以通过以下方式获得:
[i for i in A if i not in B] + [i for i in B if i not in A]
但是有没有更方便的方法可以在没有循环的情况下获得相同的结果,最好是通过 Pandas。
此致
使用集:
set(A).symmetric_difference(B)
或同等学历:
set(A)^set(B)
(如果需要,您可以转换回 list
...)
想要合并两个列表并丢弃相交的元素
A = ['a', 'b', 'c', 'd']
B = ['a', 'b', 'd', 'e', 'f']
预期结果:
['c', 'e', 'f']
我可以通过以下方式获得:
[i for i in A if i not in B] + [i for i in B if i not in A]
但是有没有更方便的方法可以在没有循环的情况下获得相同的结果,最好是通过 Pandas。
此致
使用集:
set(A).symmetric_difference(B)
或同等学历:
set(A)^set(B)
(如果需要,您可以转换回 list
...)