【问题标题】:What's the difference between .union and | for sets in python?.union 和 | 有什么区别?用于python中的集合?
【发布时间】:2021-10-04 07:55:12
【问题描述】:

.union 和 | 有什么区别?对于 python 中的集合?

>>> a = set([1, 2, 3, 4])
>>> b = set([3, 4, 5, 6])

>>> a|b
{1, 2, 3, 4, 5, 6}

>>> a.union(b)
{1, 2, 3, 4, 5, 6}

【问题讨论】:

  • 根据docsa|ba.union(b)相同。

标签: python-3.x set union frozenset


【解决方案1】:

没有区别。

其实官方python documentation about sets上面是写在一起的。

有一点区别:一个是运算符,因此它具有特定运算符的运算符优先级(例如,如果与其他集合运算符混合)。在函数情况下,函数括号明确地确定了优先级。

【讨论】:

    猜你喜欢
    • 2010-10-28
    • 2012-09-03
    • 2013-09-12
    • 1970-01-01
    相关资源
    最近更新 更多