I cannot add the integer number 1
to an existing set. In an interactive shell, this is what I am doing:
我无法将整数数字1添加到现有的集合中。在交互shell中,我所做的是:
>>> st = {'a', True, 'Vanilla'}
>>> st
{'a', True, 'Vanilla'}
>>> st.add(1)
>>> st
{'a', True, 'Vanilla'} # Here's the problem; there's no 1, but anything else works
>>> st.add(2)
>>> st
{'a', True, 'Vanilla', 2}
This question was posted two months ago, but I believe it was misunderstood. I am using Python 3.2.3.
这个问题是两个月前提出来的,但我认为是误解了。我正在使用Python 3.2.3。
4 个解决方案
#1
13
>>> 1 == True
True
I believe your problem is that 1
and True
are the same value, so 1 is "already in the set".
我认为你的问题是1和真是相同的值,所以1是“已经在集合中”。
>>> st
{'a', True, 'Vanilla'}
>>> 1 in st
True
In mathematical operations True
is itself treated as 1
:
在数学运算中,真本身被视为1:
>>> 5 + True
6
>>> True * 2
2
>>> 3. / (True + True)
1.5
Though True is a bool and 1 is an int:
虽然正确是bool, 1是int:
>>> type(True)
<class 'bool'>
>>> type(1)
<class 'int'>
Because 1 in st
returns True, I think you shouldn't have any problems with it. It is a very strange result though. If you're interested in further reading, @Lattyware points to PEP 285 which explains this issue in depth.
因为st中的1返回True,我认为你不应该有任何问题。这是一个非常奇怪的结果。如果您有兴趣进一步阅读,@Lattyware指向PEP 285,它深入地解释了这个问题。
#2
3
I believe, though I'm not certain, that because hash(1) == hash(True)
and also 1 == True
that they are considered the same elements by the set
. I don't believe that should be the case, as 1 is True
is False
, but I believe it explains why you can't add it.
我认为,虽然我不确定,因为hash(1) = hash(True)和1 == True,它们在集合中被认为是相同的元素。
#3
1
1
is equivalent to True
as 1 == True
returns true. As a result the insertion of 1
is rejected as a set cannot have duplicates.
1等于True, 1 = True返回True。因此,插入1被拒绝,因为集合不能有重复。
#4
0
Here are some link if anyone is interested in further study.
如果有人对进一步的研究感兴趣,这里有一些联系。
Is it Pythonic to use bools as ints?
用bools当ints是python语言吗?
https://*.com/a/2764099/1355722
https://*.com/a/2764099/1355722
#1
13
>>> 1 == True
True
I believe your problem is that 1
and True
are the same value, so 1 is "already in the set".
我认为你的问题是1和真是相同的值,所以1是“已经在集合中”。
>>> st
{'a', True, 'Vanilla'}
>>> 1 in st
True
In mathematical operations True
is itself treated as 1
:
在数学运算中,真本身被视为1:
>>> 5 + True
6
>>> True * 2
2
>>> 3. / (True + True)
1.5
Though True is a bool and 1 is an int:
虽然正确是bool, 1是int:
>>> type(True)
<class 'bool'>
>>> type(1)
<class 'int'>
Because 1 in st
returns True, I think you shouldn't have any problems with it. It is a very strange result though. If you're interested in further reading, @Lattyware points to PEP 285 which explains this issue in depth.
因为st中的1返回True,我认为你不应该有任何问题。这是一个非常奇怪的结果。如果您有兴趣进一步阅读,@Lattyware指向PEP 285,它深入地解释了这个问题。
#2
3
I believe, though I'm not certain, that because hash(1) == hash(True)
and also 1 == True
that they are considered the same elements by the set
. I don't believe that should be the case, as 1 is True
is False
, but I believe it explains why you can't add it.
我认为,虽然我不确定,因为hash(1) = hash(True)和1 == True,它们在集合中被认为是相同的元素。
#3
1
1
is equivalent to True
as 1 == True
returns true. As a result the insertion of 1
is rejected as a set cannot have duplicates.
1等于True, 1 = True返回True。因此,插入1被拒绝,因为集合不能有重复。
#4
0
Here are some link if anyone is interested in further study.
如果有人对进一步的研究感兴趣,这里有一些联系。
Is it Pythonic to use bools as ints?
用bools当ints是python语言吗?
https://*.com/a/2764099/1355722
https://*.com/a/2764099/1355722