如何使用集合理解在另一个集合中找到单个元素

发布于 2025-02-01 05:13:23 字数 431 浏览 2 评论 0原文

我有两个集合,set1和set2 ...除了一个元素外,这两个集合中的几乎所有元素都是相同的。

支持代码如下:

set1 = {'dog', 'cat', 'turtle', 'monkey'}
set2 = {'dog', 'cat', 'turtle', 'gorilla'}

我试图使用SET理解将每个集合的唯一元素放入其自己的变量中。

set1Unique = 'monkey'
set2Unique = 'gorilla'

我已经通过以下内容来正常工作,但是使用集合理解不是必需的。

    set1UniqueElements = set1 - set2
    set1Unique = list(set1UniqueElements)[0]

I have two sets, set1 and set2... Almost all elements in both sets are the same except for one element.

Supporting code is below:

set1 = {'dog', 'cat', 'turtle', 'monkey'}
set2 = {'dog', 'cat', 'turtle', 'gorilla'}

I am trying to get the unique element of each set into its own variable using set comprehension.

set1Unique = 'monkey'
set2Unique = 'gorilla'

I have got it working by doing the following but it is not what is required, using set comprehension is.

    set1UniqueElements = set1 - set2
    set1Unique = list(set1UniqueElements)[0]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静谧 2025-02-08 05:13:23

唯一项目是集合中不在另一组中的元素。因此:

>>> cat = 'cat'
>>> set1 = {'dog', cat, 'turtle', 'monkey'}
>>> set2 = {'dog', 'cat', 'turtle', 'gorilla'}
>>> {e for e in set2 if e not in set1}
{'gorilla'}
>>> {e for e in set1 if e not in set2}
{'monkey'}

要从集合中获取任意元素(将单元素集转换为单个元素),您可以使用set.pop()

>>> {e for e in set1 if e not in set2}.pop()
'monkey'

The unique item is the element in the set that's not in the other set. Hence:

>>> cat = 'cat'
>>> set1 = {'dog', cat, 'turtle', 'monkey'}
>>> set2 = {'dog', 'cat', 'turtle', 'gorilla'}
>>> {e for e in set2 if e not in set1}
{'gorilla'}
>>> {e for e in set1 if e not in set2}
{'monkey'}

To get an arbitrary element from the set (converting a single-element set to that single element on its own) you can use set.pop():

>>> {e for e in set1 if e not in set2}.pop()
'monkey'
岁月打碎记忆 2025-02-08 05:13:23

如果您保证了SET2中至少有一个不在SET1中的元素,而您只是在寻找第一个元素:

next(e for e in set2 if e not in set1)

此代码在set2中找到任何元素后立即停止。不在set1中。

If you are guaranteed that there is at least one element in set2 that is not in set1, and you're only looking for the first such element:

next(e for e in set2 if e not in set1)

This code stops as soon as it find any element in set2 not in set1.

森林很绿却致人迷途 2025-02-08 05:13:23

由于只有一个独特的元素,因此可以使用包装直接访问元素:

[set1Unique] = set1 - set2
[set2Unique] = set2 - set1

Since there is only a single unique element, it is possible to use packing to access the elements directly:

[set1Unique] = set1 - set2
[set2Unique] = set2 - set1
又怨 2025-02-08 05:13:23

我会使用此方法:

ex = set1 ^ set2
set1unique = ex & set1
set2unique = ex & set2

print(set1unique)
print(set2unique)

您可以在Setnunique公式中计算EX。它节省了一条线,但是您要两次计算EX。

I would use this:

ex = set1 ^ set2
set1unique = ex & set1
set2unique = ex & set2

print(set1unique)
print(set2unique)

You can can compute ex inside the setNunique formulas. It saves a line, but you compute ex twice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文