What's the difference between .union and | for sets in 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}
What's the difference between .union and | for sets in 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}
没有不同。
事实上,在关于集合的官方 python 文档中,它们是一起编写的。
有一点区别:一个是运算符,因此它具有特定运算符的运算符优先级(例如,如果与其他集合运算符混合)。在函数情况下,函数括号明确地确定了优先级。