用 python 订购东西......?
我的印象是 set() 会像 .sort() 一样对集合进行排序,
但似乎并非如此,对我来说奇怪的是它为什么对集合重新排序。
>>> h = '321'
>>> set(h)
set(['1', '3', '2'])
>>> h
'321'
>>> h = '22311'
>>> set(h)
set(['1', '3', '2'])
为什么它不返回 set(['1', '2', '3'])。我还认为,无论我使用每个数字有多少个实例,或者以什么顺序使用它们,它总是返回 set(['1', '3', '2'])。为什么?
编辑:
所以我已经阅读了你的答案,我对此的反驳是这样的。
>>> l = [1,2,3,3]
>>> set(l)
set([1, 2, 3])
>>> l = [3,3,2,3,1,1,3,2,3]
>>> set(l)
set([1, 2, 3])
为什么它对数字而不是字符串进行排序?
还
import random
l = []
for itr in xrange(101):
l.append(random.randint(1,101))
print set(l)
输出
>>>
set([1, 2, 4, 5, 6, 8, 10, 11, 12, 14, 15, 16, 18, 19, 23, 24, 25, 26, 29, 30, 31, 32, 34, 40, 43, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 69, 70, 74, 75, 77, 79, 80, 83, 84, 85, 87, 88, 89, 90, 93, 94, 96, 97, 99, 101])
I was under the impression that set() would order a collection much like .sort()
However it seems that it doesn't, what was peculiar to me was why it reorders the collection.
>>> h = '321'
>>> set(h)
set(['1', '3', '2'])
>>> h
'321'
>>> h = '22311'
>>> set(h)
set(['1', '3', '2'])
why doesn't it return set(['1', '2', '3']). I also seems that no matter how many instances of each number I user or in what order I use them it always return set(['1', '3', '2']). Why?
Edit:
So I have read your answers and my counter to that is this.
>>> l = [1,2,3,3]
>>> set(l)
set([1, 2, 3])
>>> l = [3,3,2,3,1,1,3,2,3]
>>> set(l)
set([1, 2, 3])
Why does it order numbers and not strings?
Also
import random
l = []
for itr in xrange(101):
l.append(random.randint(1,101))
print set(l)
Outputs
>>>
set([1, 2, 4, 5, 6, 8, 10, 11, 12, 14, 15, 16, 18, 19, 23, 24, 25, 26, 29, 30, 31, 32, 34, 40, 43, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 69, 70, 74, 75, 77, 79, 80, 83, 84, 85, 87, 88, 89, 90, 93, 94, 96, 97, 99, 101])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
python
set
是无序的,因此不能保证元素的排序方式与您指定的方式相同如果您想要排序的输出,请调用排序:
响应您的编辑:它会下来来执行设置。在 CPython 中,它可以归结为两件事:
1)集合将按哈希(
__hash__
函数)对极限进行模排序2)极限通常是 2 的下一个最大幂
所以让我们看一下int 情况:
对于整数,哈希值等于原始值:
因此,当所有值都是整数时,它将使用整数哈希值,一切都会顺利进行。
对于字符串表示形式,哈希值的工作方式不同:
要了解为什么 ['1','2','3'] 排序中断,请查看这些哈希值:
在我们的示例中,mod 值为 4 ( 3 elts, 2^1 = 2, 2^2 = 4) 所以
现在如果你对这个野兽进行排序,你会得到你在集合中看到的顺序:
python
set
is unordered, hence there is no guarantee that the elements would be ordered in the same way as you specify themIf you want a sorted output, then call sorted:
Responding to your edit: it comes down to the implementation of set. In CPython, it boils down to two things:
1) the set will be sorted by hash (the
__hash__
function) modulo a limit2) the limit is generally the next largest power of 2
So let's look at the int case:
for ints, the hash equals the original value:
Hence, when all the values are ints, it will use the integer hash value and everything works smoothly.
for the string representations, the hashes dont work the same way:
To understand why the sort breaks for ['1','2','3'], look at those hash values:
In our example, the mod value is 4 (3 elts, 2^1 = 2, 2^2 = 4) so
Now if you sort this beast, you get the ordering that you see in set:
来自
set
类型的 python 文档:这意味着集合没有其中元素顺序的概念。当元素以不寻常的顺序打印在屏幕上时,您不应感到惊讶。
From the python documentation of the
set
type:This means that the set doesn't have a concept of the order of the elements in it. You should not be surprised when the elements are printed on your screen in an unusual order.
Python 中的集合试图成为数学意义上的“集合”。没有重复,顺序也不重要。
A set in Python tries to be a "set" in the mathematical sense of the term. No duplicates, and order shouldn't matter.