Python set类,float和int评估 [英] Python set class, float and int evaluation

查看:105
本文介绍了Python set类,float和int评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有看到 set类的实现细节,但是我认为答案在某处。 Python分配基本上是计算右值,并使用标识符作为参考对象来指向类对象。集合也一样,即它们是抽象数据结构或参考对象的集合。集不允许重复,当我创建这样的集时:

I have not seen the implementation details of the set Class but I assume the answer to this is there somewhere. Python assignment basically evaluates rvalues and uses an identifier as reference object to point to the class object. Same for collections, i.e. they are an abstract data structure or a 'collection' of reference object. Sets don't allow duplicates and when I create a set like so:

s1 = {False, 1.0, 1, "a"} > {False, 1.0, "a"}

浮动类胜过int类,显然他们对一样。但是为什么浮动显示而不是初始化?

Float class wins over int class, obviously they evaluate to the same thing. But why does float show and init doesn't? I can't seem to find a decent answer or see it in the source.

顺便说一句,我想指出我注意到True和False将被篡改如果都使用.union()操作,则分别以某种方式代替1和0。因此,浮空看起来胜过Ints,而Ints胜过Bools。但是,

As an aside I'd like to mention I noticed that True and False will be usurped in some way in place of 1 and 0 respectively if both are present using a .union() operation. So Floats win over Ints, and Ints win over Bools it would seem. But,

>>> s1 = {False, 'a', 1}
>>> s2 = {True, 'a', 0}

>>> s1 | s2
{False, 1, 'a'} 

错误仍然存​​在。.我不知道是不是REPL问题,但是在再次测试之后,每次我不输入 {0,1,'a'} 知道发生了什么。

False remains.. I don't know if this was a REPL issue but after testing this again I get {0, 1, 'a'} every time I don't know what happened.

>>> s1 = {True, 'a', 1}
>>> s1
{1, 'a'}
>>> s2 = {False, 'a', 0}
>>> s2
{0, 'a'}

我想念什么?

>>> s1.union(s2)
{0, 1, 'a'}


推荐答案

来自 https:// docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy


布尔值type是整数类型的子类型,并且在几乎所有情况下,布尔值的行为分别类似于值0和1,不同之处在于,当转换为字符串时,分别返回字符串 False或 True 。

The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

这篇关于Python set类,float和int评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆