英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论