[英]What types the word other in sets methods refer to?
In the help of the Built-in
type set
I found the word other
在
Built-in
类型set
的帮助下,我找到了other
这个词
intersection(other, ...) set & other & ...
相交(其他,...)集和其他与...
Return a new set with elements common to the set and all others.
返回一个新集合,其中包含该集合和所有其他集合共同的元素。
My question is what are the limits of other's type, is it any iterable or any convertible to set
type? 我的问题是其他类型的限制是什么,可以迭代
set
类型吗?
Read the rest of the documentation : 阅读其余文档 :
Note, the non-operator versions of
union()
,intersection()
,difference()
, andsymmetric_difference()
,issubset()
, andissuperset()
methods will accept any iterable as an argument .请注意,
union()
,intersection()
,difference()
和symmetric_difference()
,issubset()
和issuperset()
方法的非运算符版本将接受任何可迭代的参数 。 In contrast, their operator based counterparts require their arguments to be sets.相反,其基于运算符的对应项要求将其参数设为集合。
(Emphasis mine.) (强调我的。)
Looking at the source (line 1236+) PyAnySet_Check
is called, which checks whether other
is a set
, frozenset
or subtype. 查看源 (行1236+),将调用
PyAnySet_Check
,它检查other
是set
, frozenset
还是子类型。
If this check fails, PyObject_GetIter
is called with other
as the argument in order to get an iterator. 如果此检查失败,则将
PyObject_GetIter
与other
调用,以获取迭代器。
So any iterable or object with an is-a relationship with set
or frozenset
will do. 因此,任何与
set
或frozenset
关系为is-a的可迭代对象或对象都可以。
It depends. 这取决于。 For some methods, like
intersection()
, other
can be any iterable, while for some operator based counterparts of some methods like issubset()
(operator counterpart being <=
), the other
has to be a set too. 对于某些方法,例如
intersection()
, other
可以是任意可迭代的,而对于某些方法的某些基于操作符的对应方法,例如issubset()
(操作员的对应方法为<=
), other
也必须是一个集合。 You can test for yourself too. 您也可以自己测试。 Some examples here:
这里有一些例子:
>>> s = set([1,2,3,4,5])
>>> s
{1, 2, 3, 4, 5}
>>> l = [1,3,5,7,9]
>>> l
[1, 3, 5, 7, 9]
>>> s.issubset(l)
False
>>> s <= l
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
s <= l
TypeError: unorderable types: set() <= list()
>>> s <= set(l)
False
>>> s.intersection(l)
{1, 3, 5}
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.