This question already has an answer here:
这个问题在这里已有答案:
- Python: frozensets comparison 1 answer
- Python: See if one set contains another entirely? 6 answers
Python:frozensets比较1答案
Python:看看一个集合是否完全包含另一个集合? 6个答案
How do comparison operators work? I thought they could be used only to compare numeric values, 5 <= 8, etc. But in this code sets are compared:
比较运算符如何工作?我认为它们只能用于比较数值,5 <= 8等。但是在这个代码集中进行了比较:
str = 'The quick Brow Fox'
alphabet = string.ascii_lowercase
alphaset = set(alphabet)
b = alphaset <= set(str.lower()) # Does it automatically extract length of objects?
print(len(alphaset)) # 26
print(len(set(str.lower()))) # 19
print(b)
26
15
False
I thought that it is impossible to do. alphaset <= set(str.lower())
, you know literally is, e. g. set() <= set()
. Does an operator implicitly call len()
on such objects to find some numeric values to compare?
我认为这是不可能的。 alphaset <= set(str.lower()),你知道字面意思是,e。 G。 set()<= set()。运算符是否隐式调用此类对象上的len()来查找要比较的数值?
How does it know that one sequence is bigger, less or equal etc. to another?
它是如何知道一个序列对另一个序列更大,更小或更等?
2 个解决方案
#1
0
Python supports operator overloading, which means that any class can implement methods that provide access to the standard operators.
Python支持运算符重载,这意味着任何类都可以实现提供对标准运算符的访问的方法。
For full documentation of what you can do in Python, including which methods a class can implement to support different operators, check out the Python data model.
有关您可以在Python中执行的操作的完整文档,包括类可以实现哪些方法来支持不同的运算符,请查看Python数据模型。
For a description of how a built-in type like set
implements its operators, see that type's documentation. For example, documentation for the set type.
有关像set这样的内置类型如何实现其运算符的描述,请参阅该类型的文档。例如,集类型的文档。
#2
0
From the Python manual:
从Python手册:
issubset(other)
set <= other
issubset(other)set <= other
Test whether every element in the set is in other.
测试集合中的每个元素是否在其他元素中。
There are a variety of magic methods you can implement if you want to overload operators for your own classes. When you call a < b
Python defers to a.__le__(b)
if such a method exists.
如果要为自己的类重载运算符,可以使用各种魔术方法。当你调用
#1
0
Python supports operator overloading, which means that any class can implement methods that provide access to the standard operators.
Python支持运算符重载,这意味着任何类都可以实现提供对标准运算符的访问的方法。
For full documentation of what you can do in Python, including which methods a class can implement to support different operators, check out the Python data model.
有关您可以在Python中执行的操作的完整文档,包括类可以实现哪些方法来支持不同的运算符,请查看Python数据模型。
For a description of how a built-in type like set
implements its operators, see that type's documentation. For example, documentation for the set type.
有关像set这样的内置类型如何实现其运算符的描述,请参阅该类型的文档。例如,集类型的文档。
#2
0
From the Python manual:
从Python手册:
issubset(other)
set <= other
issubset(other)set <= other
Test whether every element in the set is in other.
测试集合中的每个元素是否在其他元素中。
There are a variety of magic methods you can implement if you want to overload operators for your own classes. When you call a < b
Python defers to a.__le__(b)
if such a method exists.
如果要为自己的类重载运算符,可以使用各种魔术方法。当你调用