如何在Python中找到两个集合进行AND操作的余数

huangapple go评论64阅读模式
英文:

How to find the remainder of an AND operation on two sets in python

问题

我有两组国家,其中一组较长,我想找出在这两个列表中都没有出现的条目。

(set(a) & set(b)) 这段代码给出了在两个列表中都出现的条目。而 not(set(a) & set(b)) 只返回了 false。我正在寻找一个列表。

英文:

I have two sets of countries, one of which is longer, and I'm trying to find the entries that don't feature in both lists.

(set(a) & set(b) this code gave me the entries that appear in both. Not(set(a) & set(b)) just returned false. I'm looking for a list

答案1

得分: 2

在集合论中,这被称为对称差,在Python中,集合有一个symmetric_difference方法,所以你可以使用

set(a).symmetric_difference(set(b))

或者简写为

set(a) ^ set(b)
英文:

In set theory, this is known as the symmetric difference, and in Python, sets have a symmetric_difference method, so you could use

set(a).symmetric_difference(set(b))

or, as shorthand

set(a) ^ set(b)

huangapple
  • 本文由 发表于 2023年1月8日 22:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定