用 python 订购东西......?

发布于 2024-11-30 10:29:52 字数 1031 浏览 1 评论 0原文

我的印象是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的心境我的脸 2024-12-07 10:29:52

python set 是无序的,因此不能保证元素的排序方式与您指定的方式相同

如果您想要排序的输出,请调用排序:

sorted(set(h))

响应您的编辑:它会下来来执行设置。在 CPython 中,它可以归结为两件事:

1)集合将按哈希(__hash__ 函数)对极限进行模排序

2)极限通常是 2 的下一个最大幂

所以让我们看一下int 情况:

x=1
type(x) # int
x.__hash__() # 1

对于整数,哈希值等于原始值:

[x==x.__hash__() for x in xrange(1000)].count(False) # = 0

因此,当所有值都是整数时,它将使用整数哈希值,一切都会顺利进行。

对于字符串表示形式,哈希值的工作方式不同:

x='1'
type(x)  
# str
x.__hash__()
# 6272018864

要了解为什么 ['1','2','3'] 排序中断,请查看这些哈希值:

[str(x).__hash__() for x in xrange(1,4)]
# [6272018864, 6400019251, 6528019634]

在我们的示例中,mod 值为 4 ( 3 elts, 2^1 = 2, 2^2 = 4) 所以

[str(x).__hash__()%4 for x in xrange(1,4)]
# [0, 3, 2]
[(str(x).__hash__()%4,str(x)) for x in xrange(1,4)]
# [(0, '1'), (3, '2'), (2, '3')]

现在如果你对这个野兽进行排序,你会得到你在集合中看到的顺序:

[y[1] for y in sorted([(str(x).__hash__()%4,str(x)) for x in xrange(1,4)])]
# ['1', '3', '2']

python set is unordered, hence there is no guarantee that the elements would be ordered in the same way as you specify them

If you want a sorted output, then call sorted:

sorted(set(h))

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 limit

2) the limit is generally the next largest power of 2

So let's look at the int case:

x=1
type(x) # int
x.__hash__() # 1

for ints, the hash equals the original value:

[x==x.__hash__() for x in xrange(1000)].count(False) # = 0

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:

x='1'
type(x)  
# str
x.__hash__()
# 6272018864

To understand why the sort breaks for ['1','2','3'], look at those hash values:

[str(x).__hash__() for x in xrange(1,4)]
# [6272018864, 6400019251, 6528019634]

In our example, the mod value is 4 (3 elts, 2^1 = 2, 2^2 = 4) so

[str(x).__hash__()%4 for x in xrange(1,4)]
# [0, 3, 2]
[(str(x).__hash__()%4,str(x)) for x in xrange(1,4)]
# [(0, '1'), (3, '2'), (2, '3')]

Now if you sort this beast, you get the ordering that you see in set:

[y[1] for y in sorted([(str(x).__hash__()%4,str(x)) for x in xrange(1,4)])]
# ['1', '3', '2']
风追烟花雨 2024-12-07 10:29:52

来自 set 类型的 python 文档

集合对象是不同的可哈希对象的无序集合。

这意味着集合没有其中元素顺序的概念。当元素以不寻常的顺序打印在屏幕上时,您不应感到惊讶。

From the python documentation of the set type:

A set object is an unordered collection of distinct hashable objects.

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.

烟花肆意 2024-12-07 10:29:52

Python 中的集合试图成为数学意义上的“集合”。没有重复,顺序也不重要。

A set in Python tries to be a "set" in the mathematical sense of the term. No duplicates, and order shouldn't matter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文